Select

MySQL 8.0 - 選擇查詢

  • November 21, 2018

嗨,我在用我的表建構選擇查詢命令時遇到問題。

表客戶

+-----------------+-----------------+--------------+
| customer_number | customer_status | order_status |
+-----------------+-----------------+--------------+
|     196         |      unpaid     |    served    |
+-----------------+-----------------+--------------+
|     197         |      unpaid     |  unserved    |
+-----------------+-----------------+--------------+
|     198         |       paid      |    served    |
+-----------------+-----------------+--------------+

表 CUSTOMER_ORDERS

+-----------------+-----------------+--------------+
| cust_ord_number | customer_number | order_status |
+-----------------+-----------------+--------------+
|       350       |       196       |  preparing   |
+-----------------+-----------------+--------------+
|       351       |       196       |   pending    |
+-----------------+-----------------+--------------+
|       352       |       197       |   pending    |
+-----------------+-----------------+--------------+
|       353       |       197       |   pending    |
+-----------------+-----------------+--------------+
|       354       |       198       |   prepared   |
+-----------------+-----------------+--------------+
|       355       |       198       |   prepared   |
+-----------------+-----------------+--------------+

我想要的是所有select未付款且未提供服務且 customer_number 不得有其or from 的人。customer_number``customer_status``order_status``table customer``order_status preparing``prepared``customer_orders

基本上我需要選擇所有未付款且未提供服務且必須沒有準備或準備好的客戶編號。

對不起,如果我沒有很好地解釋它,任何答案都會有所幫助,謝謝。

SELECT *
FROM customer c
LEFT JOIN customer_orders co 
   ON c.customer_number = co.customer_number 
  AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid'
 AND c.order_status = 'unserved'
 AND co.cust_ord_number IS NULL

引用自:https://dba.stackexchange.com/questions/223121