本题来自2018年8月5日拼多多笔试题sql
- 给定两张表buy和fork分别记录用户的购买记录、收藏记录
- 返回状态“已收藏已购买”“已收藏未购买”“未收藏已购买”,以(0,1)表示
create table buy(user_id int,item_id int,buy_time DATE); create table fork(user_id int,item_id int ,fork_time DATE); insert into buy values(0001,201,'2008-09-04'); insert into buy values(0001,206,'2008-09-04'); insert into buy values(0002,203,'2008-09-04'); insert into buy values(0003,204,'2008-09-04'); insert into fork values(0001,203,'2008-09-04'); insert into fork values(0001,201,'2008-09-04'); insert into fork values(0001,205,'2008-09-04'); insert into fork values(0004,203,'2008-09-04'); insert into fork values(0003,204,'2008-09-04'); insert into fork values(0002,201,'2008-09-04');
TABLE buy
3d
TABLE fork
code
- 表格中能够发现以下问题
有些商品已购买,未收藏
有些商品未购买,已收藏htm
- 最后输出中须要汇总全部用户&商品
- 当保证buy表全部数据时,应使用LEFT JOIN
- 如有数据有购买记录,无收藏记录,表格中则会显示NULL
SELECT * FROM buy LEFT JOIN fork ON buy.user_id=fork.user_id AND buy.item_id=fork.item_id;
- 根据是否为NULL值,进行逻辑判断
CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy', CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy', CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy', CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy'
- 固然以buy为主表,是不可能出现【未收藏已购买】【未收藏未购买】的状况的。
SELECT buy.user_id,buy.item_id, CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy', CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy', CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy', CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy' FROM buy LEFT JOIN fork ON buy.user_id=fork.user_id and buy.item_id=fork.item_id
SELECT fork.user_id,fork.item_id, CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy', CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy', CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy', CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy' FROM fork LEFT JOIN buy ON buy.user_id=fork.user_id and buy.item_id=fork.item_id
- 两个结果合并后,便可获得最终结果
- UNION - 去除重复行合并
SELECT buy.user_id,buy.item_id, CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy', CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy', CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy', CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy' FROM buy LEFT JOIN fork ON buy.user_id=fork.user_id and buy.item_id=fork.item_id UNION SELECT fork.user_id,fork.item_id, CASE WHEN fork.fork_time is not null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'fork&buy', CASE WHEN fork.fork_time is not null and buy.buy_time is null THEN 1 ELSE 0 END AS 'fork&NOT buy', CASE WHEN fork.fork_time is null and buy.buy_time is not null THEN 1 ELSE 0 END AS 'NOT fork&buy', CASE WHEN fork.fork_time is null and buy.buy_time is null THEN 1 ELSE 0 END AS 'NOT fork& NOT buy' FROM fork LEFT JOIN buy ON buy.user_id=fork.user_id and buy.item_id=fork.item_id ORDER BY user_id,item_id;
【转载请注明】http://www.javashuo.com/article/p-tbhzqiac-ey.htmlblog