[英]multiple where conditions with joining
I am stuck in a problem since long. 很久以来,我一直陷入困境。
I have a table for orders
of customers, I want to get 4 types of results like 我有一张客户orders
表,我想得到4种结果,例如
1st: All orders with status=place
then 2nd: All orders with status=place and customer is new
(no previous record in order table of this customer), 然后2nd: All orders with status=place and customer is new
订单(此客户的订单表中没有以前的记录),
then 3rd: All orders with status=place and customer have at least 1 order delivered and never returned previous order
, 然后3rd: All orders with status=place and customer have at least 1 order delivered and never returned previous order
,
then 4th: All orders with status=place and customer have atleast 1 order with status=returned
然后是4th: All orders with status=place and customer have atleast 1 order with status=returned
Query for 1st step is working fine, but i got no idea to get other results.I know its like an assignment but i need help. 查询第一步工作正常,但我不知道要获得其他结果。我知道它就像一个作业,但我需要帮助。
MY Try for 1st step 我第一步的尝试
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
This give me perfect result for step one.I am using OpenCart 2.2.0.0 这给我第一步的完美结果。我正在使用OpenCart 2.2.0.0
Database Structure 数据库结构
|order_id |status | payment_code |customer_id |
|---------|-------|--------------|------------|
| 10 | place | cod | 5 |
| 11 | delvr | cod | 4 |
| 12 | return| pp_express | 5 |
| 13 |process| paytabs | 2 |
Any help or suggestion will be appreciated. 任何帮助或建议,将不胜感激。 Ask me for anything you need more. 问我您还有什么需要的。
Not sure to understand your DB structure, but I will try... 不确定是否了解您的数据库结构,但我会尝试...
Ad 2: 广告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
Accordingly, ad 4: 因此,广告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
Now for 3: 现在开始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
BTW: I strongly recommend to rename table "order" since "order" is an SQL keyword. 顺便说一句:我强烈建议重命名表“ order”,因为“ order”是一个SQL关键字。
I think you should use Union when you want to do that with one query. 我想在一个查询中要使用Union的情况。
Other ways, when same customer meets 2 contidions, then you will only see one. 其他方式,当同一位客户遇到2个竞争条件时,您只会看到一个。
https://www.w3schools.com/sql/sql_union.asp https://www.w3schools.com/sql/sql_union.asp
For the 2nd query : you can just add an AND clause to your SQL : AND user_id = 0 对于第二个查询:您可以在SQL中添加AND子句:AND user_id = 0
Assuming a new order is inserted with user_id = 0 if this is a new user 如果这是一个新用户,则假定插入了一个新订单,其中user_id = 0
queries: the queries are as per your question u asked and you orders table structure(i am considering that you have not created any column like created_at and updated_at). 查询:查询是根据您所问的问题,并为表结构排序(我正在考虑尚未创建诸如created_at和updated_at之类的任何列)。
select * from orders where status = 'place'; 从状态=“地点”的订单中选择*;
select orders.order_id ,orders.customer_id from orders where status = 'place' GROUP BY order_id, customer_id HAVING COUNT(customer_id) = 1; 从状态为'place'的订单中选择orders.order_id,orders.customer_id。GROUP BY order_id,customer_id HAVING COUNT(customer_id)= 1;
select a.order_id, a.customer_id from orders a where a.status in (select b.status from orders b where a.order_id = b.order_id and status <> 'return') and a.status = 'place' GROUP BY a.order_id, a.customer_id HAVING COUNT(a.customer_id) >= 1; 从订单a中选择a.order_id,a.customer_id(在其中a.status处选择)(从订单b中选择b.status,其中a.order_id = b.order_id和状态<>'return')和a.status ='place'GROUP BY a.order_id,a.customer_id HAVING COUNT(a.customer_id)> = 1;
select a.order_id, a.customer_id from orders a where a.status in (select b.status from orders b where a.order_id = b.order_id and status = 'return') and a.status = 'place' GROUP BY a.order_id, a.customer_id HAVING COUNT(a.customer_id) >= 1 从订单a中选择a.order_id,a.customer_id(其中a.status位于)(从订单b中选择b.status,其中a.order_id = b.order_id并且status ='return')和a.status ='place'GROUP BY a .order_id,a.customer_id有COUNT(a.customer_id)> = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.