Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
| unit_price | int |
+--------------+---------+
product_id 是這張表的主鍵
Table: Sales
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
+------ ------+---------+
這個表沒有主鍵衣撬,它可以有重復(fù)的行.
product_id 是 Product 表的外鍵.
編寫一個 SQL 查詢摩疑,查詢購買了 S8 手機(jī)卻沒有購買 iPhone 的買家。注意這里 S8 和 iPhone 是 Product 表中的產(chǎn)品嵌器。
查詢結(jié)果格式如下圖表示:
Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1 | S8 | 1000 |
| 2 | G4 | 800 |
| 3 | iPhone | 1400 |
+------------+--------------+------------+
Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1 | 1 | 1 | 2019-01-21 | 2 | 2000 |
| 1 | 2 | 2 | 2019-02-17 | 1 | 800 |
| 2 | 1 | 3 | 2019-06-02 | 1 | 800 |
| 3 | 3 | 3 | 2019-05-13 | 2 | 2800 |
+-----------+------------+----------+------------+----------+-------+
Result table:
+-------------+
| buyer_id |
+-------------+
| 1 |
+-------------+
id 為 1 的買家購買了一部 S8粗梭,但是卻沒有購買 iPhone并鸵,而 id 為 3 的買家卻同時購買了這 2 部手機(jī)。
解答
想法是創(chuàng)建購買了S8的臨時表 再創(chuàng)建沒有購買iphone的臨時表 兩表連接即可
先選出 S8 iphone的id
select product_id
from Product
where product_name = 'S8';
select product_id
from Product
where product_name = 'iPhone';
選出購買了S8的買家
select buyer_id
from Sales
where product_id = (select product_id
from Product
where product_name = 'S8')
選出沒有購買iphone的id
select buyer_id
from Sales
where product_id <> (select product_id
from Product
where product_name = 'iPhone')
兩表連接
SELECT tmp.buyer_id AS buyer_id
FROM (SELECT buyer_id
FROM Sales
WHERE product_id = (SELECT product_id
FROM Product
WHERE product_name = 'S8')) tmp
JOIN (SELECT buyer_id
FROM Sales
WHERE product_id <> (SELECT product_id
FROM Product
WHERE product_name = 'iPhone')) tmp2
ON tmp.buyer_id = tmp2.buyer_id
也可以轉(zhuǎn)為集合差的問題 從買了S8的集合中刪去買了iphone的集合
select A.buyer_id
from
(
select distinct buyer_id
from Product as P join Sales as S
on(P.product_id = S.product_id and P.product_name ='S8')
) as A
left join
(
select distinct buyer_id
from Product as P join Sales as S
on(P.product_id = S.product_id and P.product_name ='iPhone')
) as B
on(A.buyer_id = B.buyer_id)
where B.buyer_id is NULL
別的解答
先做兩表連接
SELECT *
FROM Sales AS S LEFT JOIN Product AS P
ON(P.product_id = S.product_id)
對每個買家統(tǒng)計購買S8和Iphone的數(shù)量
SELECT SUM(IF(P.`product_name` = 'S8', 1, 0)), SUM(IF(P.`product_name` = 'iPhone', 1, 0))
FROM Sales AS S
LEFT JOIN Product AS P
ON(P.product_id = S.product_id)
GROUP BY S.`seller_id`
再選出購買S8的數(shù)量>0 而購買iPhone數(shù)量=0的買家id即可
SELECT S.`buyer_id`
FROM Sales AS S
LEFT JOIN Product AS P
ON(P.product_id = S.product_id)
GROUP BY S.`seller_id`
HAVING SUM(IF(P.`product_name` = 'S8', 1, 0)) > 0 AND SUM(IF(P.`product_name` = 'iPhone', 1, 0)) = 0