我有一个带有子查询的查询。两者都返回用户ID列表。在内部查询中,我给出了在给定时间范围内在特定位置使用现金的所有客户。
第二个,选择在特定日期之后订购的第一个用户ID,并且不使用现金作为付款方式。
select distinct c.user_id
from (
select distinct o.user_id
from `order` as o
inner join payments as p on p.id = o.id
where o.orderplaced_ts > "2016-01-01 00:00:00"
and o.store_id = "12"
and p.payment_method = "Cash"
) as c
inner join `order` as o on c.user_id = o.user_id
inner join `payments` as p on o.id = p.id
where o.orderplaced_ts > "2016-03-13 00:00:00"
and o.store_id = "12"
and p.payment_method != "Cash"
如何在2016-03-13
以后没有订单的用户之间进行交叉。
供参考:
select distinct o.user_id
from `order` as o
inner join payments as p on p.id = o.id
where o.orderplaced_ts > "2016-01-01 00:00:00"
and o.store_id = "12"
and p.payment_method = "Cash"
返回236个唯一ID - 调用此集A
两个查询组合返回160唯一ID - 调用此集B
集合A中的哪些ID在集合B中是not
答案 0 :(得分:0)
我更愿意从你这边得到一些方法。
但是既然我没有,这是我的尝试:
select distinct c.user_id
from (
select distinct o.user_id
from `order` as o
inner join payments as p on p.id = o.id
where o.orderplaced_ts > "2016-01-01 00:00:00"
and o.store_id = "12"
and p.payment_method = "Cash"
) as c
LEFT JOIN `order` as o
ON c.user_id = o.user_id
AND o.orderplaced_ts > "2016-03-13 00:00:00"
AND o.store_id = "12"
LEFT JOIN `payments` as p
ON o.id = p.id
AND p.payment_method != "Cash"
WHERE p.id IS NULL
答案 1 :(得分:0)
在两个查询之间使用not in子句
select distinct o.user_id
from `order` as o
inner join payments as p on p.id = o.id
where o.orderplaced_ts > "2016-01-01 00:00:00"
and o.store_id = "12"
and p.payment_method = "Cash"
and o.user_id not in (
select distinct c.user_id
from (
select distinct o.user_id
from `order` as o
inner join payments as p on p.id = o.id
where o.orderplaced_ts > "2016-01-01 00:00:00"
and o.store_id = "12"
and p.payment_method = "Cash"
) as c
inner join `order` as o on c.user_id = o.user_id
inner join `payments` as p on o.id = p.id
where o.orderplaced_ts > "2016-03-13 00:00:00"
and o.store_id = "12"
)