如何获得没有发票号的订单号

时间:2014-05-07 15:21:16

标签: mysql

如何找到没有发票编号的订单号?我做了以下查询,它显示了所有包含发票号的订单。如何获得没有发票号的所有订单号?

SELECT invoice_tbl.increment_id AS 'Sales_Invoice_Number', order_tbl.increment_id AS   'Sales Order Number'
FROM magentodb.sales_flat_invoice AS invoice_tbl
JOIN magentodb.sales_flat_order AS order_tbl
  ON (order_tbl.entity_id = invoice_tbl.order_id)
WHERE order_tbl.created_at >= '2014-03-17'
  AND order_tbl.created_at <= '2014-05-07'

2 个答案:

答案 0 :(得分:0)

您可以使用NOT EXISTS子句。

SELECT  order_tbl.increment_id AS   'Sales Order Number'
FROM  magentodb.sales_flat_order AS order_tbl

WHERE order_tbl.created_at >= '2014-03-17'
  AND order_tbl.created_at <= '2014-05-07'
  AND NOT EXISTS (SELECT NULL FROM magentodb.sales_flat_invoices
                  WHERE order_tbl.entity_id = order_id)

LEFT JOIN sales_flat_invoice 使用where子句检查null order_id也可以做到这一点。

答案 1 :(得分:0)

 Select order_tbl.increment_id AS 'Sales Order Number'
 FROM magentodb.sales_flat_order AS order_tbl
 WHERE order_tbl.increment_id NOT IN (select invoice_tbl.order_id 
                                      FROM magentodb.sales_flag_invoice AS invoice_tbl)
 AND order_tbl.created_at >= '2014-03-17'
 AND order_tbl.created_at <= '2014-05-07'

这将从order_tbl中选择发票表中没有关联行的所有订单,并将原始过滤保留在查询的底部。