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)
);
如何查找客户至少被访问过2次的商店地址,名称为“john”?
从商店中选择地址自然联合访问哪里是CUSID(从客户中选择CUST = CEND ='john'GROUP BY CUSID HAVING COUNT(CUSID)> 2);
我尝试了很多种连接,似乎在我将count和equal条件放在一起之后,我的结果将是0行。
答案 0 :(得分: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