2017-02-24 3 views
1

목록 항목

조직 테이블

===================== 
| id |  name  | 
-----+----------------+ 
| 1 | Organization 1 | 
===================== 

탑재 대

================================= 
| id | name | organization_id | 
-----+----------+-----------------+ 
| 1 | Place 1 |  1  | 
-----+----------+-----------------+ 
| 2 | Place 2 |  1  | 
-----+----------+-----------------+ 
| 3 | Place 3 |  1  | 
================================= 

프로파일 테이블

이러한 테이블 구조를 상상해
============= 
| username | 
-------------- 
| [email protected] | 
-------------- 
| [email protected] | 
============= 

Visitedplace 테이블

================================================ 
| id | place_id | profile_username | visiteddate | 
-----+----------+---------------- -+-------------+ 
| 1 |  1 | [email protected] | 2017-01-01 | 
-----+----------+------------------+-------------+ 
| 2 |  2 | [email protected] | 2017-02-01 | 
-----+----------+------------------+-------------+ 
| 3 |  1 | [email protected] | 2017-01-15 | 
================================================ 

다음은 테이블을 만들고 여기에 데이터를 삽입하는 SQL 문에서도 있습니다

-- ---------------------------- 
-- Table structure for organization 
-- ---------------------------- 
DROP TABLE IF EXISTS "organization"; 
CREATE TABLE "organization" (
"id" int4 NOT NULL, 
"name" varchar(255) COLLATE "default" NOT NULL 
) WITH (OIDS=FALSE); 
-- ---------------------------- 
-- Records of organization 
-- ---------------------------- 
BEGIN; 
INSERT INTO "organization" VALUES ('1', 'Organization 1'); 
COMMIT; 
-- ---------------------------- 
-- Table structure for place 
-- ---------------------------- 
DROP TABLE IF EXISTS "place"; 
CREATE TABLE "place" (
"id" int4 NOT NULL, 
"name" varchar(255) COLLATE "default" NOT NULL, 
"organization_id" int4 NOT NULL 
)WITH (OIDS=FALSE); 
-- ---------------------------- 
-- Records of place 
-- ---------------------------- 
BEGIN; 
INSERT INTO "place" VALUES ('1', 'Place 1', '1'); 
INSERT INTO "place" VALUES ('2', 'Place 2', '1'); 
INSERT INTO "place" VALUES ('3', 'Place 3', '1'); 
COMMIT; 
-- ---------------------------- 
-- Table structure for profile 
-- ---------------------------- 
DROP TABLE IF EXISTS "profile"; 
CREATE TABLE "profile" (
"username" varchar(255) COLLATE "default" NOT NULL 
)WITH (OIDS=FALSE); 
-- ---------------------------- 
-- Records of profile 
-- ---------------------------- 
BEGIN; 
INSERT INTO "profile" VALUES ('[email protected]'); 
INSERT INTO "profile" VALUES ('[email protected]'); 
COMMIT; 
-- ---------------------------- 
-- Table structure for visitedplace 
-- ---------------------------- 
DROP TABLE IF EXISTS "visitedplace"; 
CREATE TABLE "visitedplace" (
"id" int4 NOT NULL, 
"place_id" int4 NOT NULL, 
"profile_username" varchar(255) COLLATE "default" NOT NULL, 
"visiteddate" date NOT NULL 
)WITH (OIDS=FALSE); 
-- ---------------------------- 
-- Records of visitedplace 
-- ---------------------------- 
BEGIN; 
INSERT INTO "visitedplace" VALUES ('1', '1', '[email protected]', '2017-02-24'); 
COMMIT; 
-- ---------------------------- 
-- Alter Sequences Owned By 
-- ---------------------------- 
-- ---------------------------- 
-- Primary Key structure for table organization 
-- ---------------------------- 
ALTER TABLE "organization" ADD PRIMARY KEY ("id"); 
-- ---------------------------- 
-- Primary Key structure for table place 
-- ---------------------------- 
ALTER TABLE "place" ADD PRIMARY KEY ("id"); 
-- ---------------------------- 
-- Primary Key structure for table profile 
-- ---------------------------- 
ALTER TABLE "profile" ADD PRIMARY KEY ("username"); 
-- ---------------------------- 
-- Primary Key structure for table visitedplace 
-- ---------------------------- 
ALTER TABLE "visitedplace" ADD PRIMARY KEY ("id"); 
-- ---------------------------- 
-- Foreign Key structure for table "place" 
-- ---------------------------- 
ALTER TABLE "place" ADD FOREIGN KEY ("organization_id") REFERENCES 
"organization" ("id") ON DELETE CASCADE ON UPDATE CASCADE; 
------------------------------ 
-- Foreign Key structure for table "visitedplace" 
-- ---------------------------- 
ALTER TABLE "visitedplace" ADD FOREIGN KEY ("profile_username") REFERENCES "profile" ("username") ON DELETE CASCADE ON UPDATE CASCADE; 
ALTER TABLE "visitedplace" ADD FOREIGN KEY ("place_id") REFERENCES "place" ("id 

") ON DELETE CASCADE ON UPDATE CASCADE; 

내 질문

내가이 다음을 실행

검색어

01 23,516,
select profile.username, count(distinct place.id) 
    from profile profile 
    left outer join visitedplace visitedplace on profile.username=visitedplace.profile_username 
    inner join place place on visitedplace.place_id=place.id 
    where place.organization_id=1 
    group by profile.username 

는 다음 결과

===================== 
| username | count | 
-------------+------- 
| [email protected] | 2 | 
===================== 

를 반환하지만 난 다음 결과

===================== 
| username | count | 
-------------+------- 
| [email protected] | 2 | 
-------------+------- 
| [email protected] | 0 | 
===================== 

을 기대하고 그래서 내가 기대하고 무엇을 나에게 반환하는 쿼리를 할 수 있습니까?

나는 left join를 사용하여 포스트 그레스

+0

정말 (다소) 오래된 버전을 사용하고 있습니까? –

+0

@a_horse_with_no_name ur 질문을 얻을 수 없다, pls, thnx를 명확히 할 수있다 –

답변

1

사용되어 사용하고 있습니다. 따라서 모든 조인은 외부 조인이어야합니다 (첫 번째 조인 이후). 그리고, 당신은 where 절에주의해야한다 : 당신이 조심하지 않으면

select p.username, count(distinct place.id) 
from profile p left outer join 
    visitedplace vp 
    on p.username = vp.profile_username left join 
    place pl 
    on vp.place_id = pl.id and pl.organization_id = 1 
group by p.username; 

후 외부는 inner join에 교대로 가입 할 수 있습니다.

+0

네, 지금은 고마워. :) –