加入的多个where条件
multiple where conditions with joining
我一直被一个问题困住了。
我有 table 个 orders
客户,我想得到 4 种类型的结果,例如
1st: All orders with status=place
然后2nd: All orders with status=place and customer is new
(此客户之前订单table没有记录),
然后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
第 1 步的查询工作正常,但我不知道如何让其他 results.I 知道这就像一项任务,但我需要帮助。
我的尝试第一步
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
这给了我完美的结果 one.I 我正在使用 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 |
如有任何帮助或建议,我们将不胜感激。
向我要任何你需要的东西。
我认为当你想用一个查询做到这一点时,你应该使用 Union。
其他方式,当同一客户满足2个条件时,您只会看到一个。
对于第二个查询:您只需在 SQL 中添加一个 AND 子句:
AND user_id = 0
假设插入新订单 user_id = 0 如果这是新用户
不确定是否了解您的数据库结构,但我会尝试...
广告 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
顺便说一句:我强烈建议重命名 table "order" 因为 "order" 是一个 SQL 关键字。
查询:
查询是根据您提出的问题进行的,您订购了 table 结构(我认为您没有创建任何列,例如 created_at 和 updated_at)。
select * 来自状态 = 'place' 的订单;
select orders.order_id ,orders.customer_id 来自订单状态 = 'place' GROUP BY order_id, customer_id HAVING计数(customer_id) = 1;
select a.order_id, a.customer_id 来自订单 a where a.status in (select b.status 来自订单 b其中 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;
select a.order_id, a.customer_id 来自订单 a where a.status in (select b.status 来自订单 b其中 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
我一直被一个问题困住了。
我有 table 个 orders
客户,我想得到 4 种类型的结果,例如
1st: All orders with status=place
然后2nd: All orders with status=place and customer is new
(此客户之前订单table没有记录),
然后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
第 1 步的查询工作正常,但我不知道如何让其他 results.I 知道这就像一项任务,但我需要帮助。
我的尝试第一步
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
这给了我完美的结果 one.I 我正在使用 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 |
如有任何帮助或建议,我们将不胜感激。 向我要任何你需要的东西。
我认为当你想用一个查询做到这一点时,你应该使用 Union。
其他方式,当同一客户满足2个条件时,您只会看到一个。
对于第二个查询:您只需在 SQL 中添加一个 AND 子句: AND user_id = 0
假设插入新订单 user_id = 0 如果这是新用户
不确定是否了解您的数据库结构,但我会尝试...
广告 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
顺便说一句:我强烈建议重命名 table "order" 因为 "order" 是一个 SQL 关键字。
查询: 查询是根据您提出的问题进行的,您订购了 table 结构(我认为您没有创建任何列,例如 created_at 和 updated_at)。
select * 来自状态 = 'place' 的订单;
select orders.order_id ,orders.customer_id 来自订单状态 = 'place' GROUP BY order_id, customer_id HAVING计数(customer_id) = 1;
select a.order_id, a.customer_id 来自订单 a where a.status in (select b.status 来自订单 b其中 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;
select a.order_id, a.customer_id 来自订单 a where a.status in (select b.status 来自订单 b其中 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