我有一家网上商店,有一个订单DB,如下所示:
id purchase_date buyer_name buyer_email product_name delivery_status
1 10.09.2014 jo smith jo@smith.com dildo delivered
2 10.09.2014 jo smith jo@smith.com superdildo delivered
3 11.09.2014 john lol jo@lol.com cream delivered
4 13.09.2014 john lol jo@lol.com supercream not delivered
5 15.09.2014 john doe john@doe.com lingerie delivered
6 15.09.2014 john doe john@doe.com lingerie2 not delivered
7 15.09.2014 feels no ff@trol.com supercream delivered
8 18.09.2014 jo smith jo@smith.com cream not delivered我想选择所有不同的买方_电子邮件从这个表,其中所有客户的订单在这一天是“交付”。
我的意思是:
ID 1和2将是匹配的,并且查询应该输出jo@smith.com,因为他在这一天所做的这两项订单都是交付的。
ID 3也是匹配的,因为jo@lol.com当天(11.09.2014)下的所有订单都已交付。
查询中将不匹配ID 4(该名称上当天的所有订单并非都已交付)
ID 5和6也不匹配。
ID 7匹配
ID 8不匹配。
发布于 2014-11-24 12:41:51
您需要一个带有having子句的聚合。至少开始吧。以下是已完全交付订单的日期和买家:
select o.purchase_date, o.buyer_email
from orders o
group by o.purchase_date, o.buyer_email
having sum(delivery_status <> 'delivered') = 0;如果您想要订单ids,最简单的方法是使用group_concat()
select o.purchase_date, o.buyer_email, group_concat(o.id) as ids
from orders o
group by o.purchase_date, o.buyer_email
having sum(delivery_status <> 'delivered') = 0;如果想要完整的行,可以使用join。
发布于 2014-11-24 12:41:00
select distinct buyer_email
from your_table
group by buyer_email, purchase_date
having sum(delivery_status <> 'delivered') = 0https://stackoverflow.com/questions/27105074
复制相似问题