2013-05-06 56 views
-3
CREATE TABLE CUSTOMER (
CUSID VARCHAR(25) NOT NULL, 
CNAME VARCHAR(50), 
CONSTRAINT CUSTOMER_PKEY PRIMARY KEY (CUSID), 
); 

CREATE TABLE SHOP (
SHOPID VARCHAR(10) NOT NULL, 
ADDRESS VARCHAR(25), 
CONSTRAINT SHOP_PKEY PRIMARY KEY (SHOPID), 
); 

CREATE TABLE VISIT (
CUSID VARCHAR(25) NOT NULL, 
SHOPID VARCHAR(10) NOT NULL, 
VDATE DATE  NOT NULL, 
CONSTRAINT VISIT_PKEY PRIMARY KEY (CUSID, SHOPID, VDATE), 
CONSTRAINT VISIT_FKEY1 FOREIGN KEY (CUSID) REFERENCES CUSTOMER(CUSID), 
CONSTRAINT VISIT_FKEY2 FOREIGN KEY (SHOPID) REFERENCES SHOP(SHOPID) 
); 

如何找到已被訪問過至少兩次由名爲'john'的客戶訪問的商店的地址? (CUSID> 2)的CNAME ='約翰'GROUP BY CUSID的SELECT CUSID(CUSID FROM CUSTOMER WHERE CNAME ='john'GROUP BY CUSID HAVING COUNT(CUSID)> 2);選擇地址從商店天然加入訪問。具有計數條件和等條件的Oracle Sql 3表

我已經嘗試了很多種連接,似乎在我把count和equal條件放在一起之後,我的結果將會是0行。

+0

提示連接表和用戶的WHERE子句限制 – Randy 2013-05-06 12:22:41

+2

的SO的目的是建立答案的存儲庫中的行*編程*這將是感興趣的其他人的問題。這不是代理服務的功課。 – APC 2013-05-06 12:25:12

+0

選擇地址從商店天然加入訪問在哪裏CUSID(選擇CUSID從客戶那裏CNAME ='約翰'GROUP BY CUSID具有COUNT(CUSID)> 2); – user2351750 2013-05-06 12:29:25

回答

1
SELECT DISTINCT s.address 
    FROM shop s 
    JOIN visit v ON s.shopid = v.shopid 
    JOIN customer c ON v.customerid = c.customerid 
    WHERE c.cname = 'John' 
    GROUP BY 
    s.address 
    , c.customerid 
    HAVING COUNT(*) > 1 
+0

非常感謝...搞亂了我的團隊,並有邏輯......做了很多那裏有錯誤。 – user2351750 2013-05-06 12:49:48

+0

我添加了一個小的更正,以便查詢不會返回重複的商店名稱 – kyooryu 2013-05-06 13:06:39