有哪些类型的网站,中国建设银行潍坊市分行官方网站,免费婚纱网站模板,wordpress快速编辑添加多个标签思路#xff1a;
思路1#xff1a;买了A#xff0c;买了B#xff0c;没有买C。
按人分组统计#xff0c;A的数0, B的数0 ,C的数 0。
思路2#xff1a;反过来查#xff0c;用户id。在产品表里,产品名为A#xff0c;为B的用户列表里#xff0c;但是不在产品…思路
思路1买了A买了B没有买C。
按人分组统计A的数0, B的数0 ,C的数 0。
思路2反过来查用户id。在产品表里,产品名为A为B的用户列表里但是不在产品表里,产品为C的用户列表里。 错误答案 select c.customer_id,c.customer_name,o.product_name from Customers c left join Orders o on c.customer_id o.customer_id group by c.customer_id ,c.customer_name,o.product_name having sum(if(o.product_name A,1,0)) 0 and sum(if(o.product_name B,1,0)) 0 and sum(if(o.product_name C,1,0)) 0 其实这一句SQL执行select c.customer_id,c.customer_name,o.product_name from Customers c left join Orders o on c.customer_id o.customer_id
group by c.customer_id ,c.customer_name,o.product_name得到的结果是 如果再加上 having sum(if(o.product_name A,1,0)) 0 and sum(if(o.product_name B,1,0)) 0 and sum(if(o.product_name C,1,0)) 0
结果是空的。 因为它是对每一行的记录去叠加执行这些条件等于A等于B不等于C比较的。
很显然不可能有一行记录同时满足等于A等于B不等于C。
除非把product_name 这列 隐藏。根据customer_id 和customer_name去分组这样他们的product_name 就会被聚合起来虽然我们看不见 product_name 但是我们却可以对它进行统计。好神奇~ 参考答案 select c.customer_id,c.customer_name from Customers c left join Orders o on c.customer_id o.customer_id group by c.customer_id ,c.customer_name having sum(if(o.product_name A,1,0)) 0 and sum(if(o.product_name B,1,0)) 0 and sum(if(o.product_name C,1,0)) 0