LeetCode(数据库)- 销售分析 II
Posted Lux_Sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(数据库)- 销售分析 II相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略。
解题思路:注意:COUNT(NULL) = 0、COUNT(非NULL) = 1。
AC 代码
-- 解决方案(1)
SELECT S.buyer_id
FROM Sales S JOIN Product P
ON S.product_id = P.product_id
GROUP BY S.buyer_id
HAVING COUNT(IF(P.product_name = 'S8',TRUE, NULL)) >= 1 AND COUNT(IF(P.product_name = 'iPhone',TRUE, NULL)) = 0
-- 解决方案(2)
SELECT DISTINCT RS1.buyer_id
FROM (SELECT buyer_id
FROM Sales s JOIN Product p ON s.product_id = p.product_id
WHERE product_name = 'S8') RS1 LEFT JOIN
(SELECT buyer_id
FROM Sales s JOIN Product p ON s.product_id = p.product_id
WHERE product_name = 'iPhone') RS2 ON RS1.buyer_id = RS2.buyer_id
WHERE RS2.buyer_id IS NULL
以上是关于LeetCode(数据库)- 销售分析 II的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode]1083. 销售分析 II(Mysql,having+if)
LeetCode:Database 40.产品销售分析 II
LeetCode-面试算法经典-Java实现106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)(示例(代码片