我在MySQL中有兩個表:
表1 - WORKORDERS
ID | 數量 |
---|---|
1 | 2 |
2 | 1 |
表2 - ITEMSINWORKORDERS
ID | |
---|---|
1 | |
2 | |
3 |
我有一個查詢:
SELECT WORKORDERS.ID , WORKORDERS.QUANTITY AS NOMINAL_QTY , COUNT(ITEMSINWORKORDERS.WORKORDER) AS ENTERED_QTY FROM WORKORDERS JOIN ITEMSINWORKORDERS ON ITEMSINWORKORDERS.WORKORDER = WORKORDERS.ID WHERE WORKORDERS.QUANTITY > ( SELECT COUNT(ITEMSINWORKORDERS.WORKORDER ) FROM WORKORDERS INNER JOIN ITEMSINWORKORDERS ON ITEMSINWORKORDERS.WORKORDER = WORKORDERS.ID )
最初,我在語句中使用了左外連接,所以我將其改為內連接,希望得到一個空集。如何使其在沒有任何缺失條目的工作訂單時返回一個空集合?
ID | NOMINAL_QTY | |
---|---|---|
NULL | NULL |
最初,我在語句中使用了左外連接,所以我將其改為內連接,希望得到一個空集。
補充:我嘗試使用NULLIF來解決這個問題,如下:
SELECT WORKORDERS.ID , WORKORDERS.QUANTITY AS NOMINAL_QTY , NULLIF(COUNT(ITEMSINWORKORDERS.WORKORDER), 0) AS ENTERED_QTY FROM WORKORDERS JOIN ITEMSINWORKORDERS ON ITEMSINWORKORDERS.WORKORDER = WORKORDERS.ID WHERE WORKORDERS.QUANTITY > ( SELECT COUNT( ITEMSINWORKORDERS.WORKORDER ) FROM WORKORDERS INNER JOIN ITEMSINWORKORDERS ON ITEMSINWORKORDERS.WORKORDER = WORKORDERS.ID )
ID | NOMINAL_QTY | |
---|---|---|
NULL | NULL |