带连接的多个where条件

w41d8nur  于 2021-06-20  发布在  Mysql
关注(0)|答案(4)|浏览(351)

我陷入了一个问题很久了。
我有一张table给你 orders 对于客户,我想得到4种类型的结果,如 1st: All orders with status=place 那么 2nd: All orders with status=place and customer is new (此客户的订单表中没有以前的记录),
那么 3rd: All orders with status=place and customer have at least 1 order delivered and never returned previous order ,
那么 4th: All orders with status=place and customer have atleast 1 order with status=returned 第一步的查询工作正常,但我不知道如何得到其他结果。我知道这就像一个作业,但我需要帮助。
我的第一步尝试

SELECT o.order_id, o.customer_id, o.order_status_id, 
FROM `order` o  
WHERE (o.order_status_id = '1' 
AND o.payment_code <> 'paytabs' 
AND o.payment_code <> 'pp_express') 
ORDER BY o.order_id DESC

这给了我第一步的完美结果。我使用的是opencart 2.2.0.0
数据库结构

|order_id |status | payment_code |customer_id |
|---------|-------|--------------|------------|
| 10      | place | cod          |   5        |
| 11      | delvr | cod          |   4        |
| 12      | return| pp_express   |   5        |
| 13      |process| paytabs      |   2        |

任何帮助或建议都将不胜感激。你还需要什么就问我。

sf6xfgos

sf6xfgos1#

我认为当你想用一个查询来实现这一点时,应该使用union。
另一方面,当同一个顾客遇到两个顾客时,你只会看到一个。
https://www.w3schools.com/sql/sql_union.asp

1wnzp6jl

1wnzp6jl2#

不知道你的数据库结构,但我会尝试。。。
广告2:

SELECT o.order_id, o.customer_id, o.order_status_id, 
FROM `order` o 
WHERE (o.order_status_id = '1' 
AND o.payment_code <> 'paytabs' 
AND o.payment_code <> 'pp_express') 
AND o.customer_id NOT IN (SELECT o2.customer_id from order o2 WHERE o2.order_id != o.order_id AND o2.customer_id = o.customer_id)
ORDER BY o.order_id DESC

因此,广告4:

SELECT o.order_id, o.customer_id, o.order_status_id, 
FROM `order` o 
WHERE (o.order_status_id = '1' 
AND o.payment_code <> 'paytabs' 
AND o.payment_code <> 'pp_express') 
AND o.customer_id IN (SELECT o2.customer_id from order o2 WHERE o2.order_status = 'returned')
ORDER BY o.order_id DESC

现在为3:

SELECT o.order_id, o.customer_id, o.order_status_id, 
FROM `order` o 
WHERE (o.order_status_id = '...?') 
AND o.customer_id NOT IN (SELECT o2.customer_id from order o2 WHERE o2.order_status = 'returned')
AND o.customer_id IN (SELECT o3.customer_id from order o3 WHERE o3.order_status = 'delivered')
ORDER BY o.order_id DESC

顺便说一句:我强烈建议重命名表“order”,因为“order”是一个sql关键字。

n7taea2i

n7taea2i3#

对于第二个查询:您只需向sql:and user\u id=0添加一个and子句
如果这是一个新用户,则假定插入了一个用户id=0的新订单

ogq8wdun

ogq8wdun4#

查询:查询是根据你的问题,你问和订单表结构(我认为你没有创建任何列,如创建和更新在)。
选择*from orders where status='地点';
选择orders.order\u id,orders.customer\u id from orders where status='place'group by order\u id,customer\u id having count(customer\u id)=1;
选择a.order\u id,a.customer\u id from orders a where a.status in(选择b.status from orders b where a.order\u id=b.order\u id and status<>'return')和a.status='place按a.order\u id分组,a.customer\u id having count(a.customer\u id)>=1;
选择a.order\u id,a.customer\u id from orders a where a.status in(选择b.status from orders b where a.order\u id=b.order\u id and status='return')和a.status='place,按a.order\u id分组,a.customer\u id having count(a.customer\u id)>=1

相关问题