执行左连接时出现未知列错误

时间:2018-02-06 15:57:09

标签: mysql sql left-join

我正在尝试让左连接工作并出现错误 'on clause

中的未知列'sales_order_grid.entity_id'

但是我不知道如何重新查询列sales_order_grid确实包含名为entity_id的列?我正在尝试运行一个显示结果的查询,即使sales_order_tax表没有匹配的行我希望其他表中的其余数据显示。

SELECT sales_order_grid.entity_id, sales_order_grid.created_at, sales_order_grid.increment_id AS OrderID, sales_order_grid.status, sales_order_grid.payment_method AS payment_method, sales_order_grid.base_grand_total AS pmt, sales_order_grid.base_grand_total AS charged, t.code AS tax_code, t.title AS taxrate, t.amount AS taxamount, 
      sales_order_grid.shipping_and_handling AS shipping_invoice, sales_order_grid.grand_total AS total_invoice, customer_address_entity.firstname, customer_address_entity.lastname, 
      customer_address_entity.city, customer_address_entity.region, customer_address_entity.postcode 
FROM sales_order_grid, customer_address_entity LEFT OUTER JOIN sales_order_tax AS t on sales_order_grid.entity_id = t.order_id
WHERE sales_order_grid.created_at >= '2018-02-01 00:00:00' 
  AND sales_order_grid.created_at <= '2018-02-05 23:59:59' 
  AND sales_order_grid.status IN('Paid','complete','closed','Processing','on hold') 
  AND sales_order_grid.customer_id = customer_address_entity.parent_id    
GROUP BY sales_order_grid.entity_id

1 个答案:

答案 0 :(得分:2)

您在加入表格时混合了两种不同的方式。

您的查询应如下所示:

SELECT sales_order_grid.entity_id, sales_order_grid.created_at, sales_order_grid.increment_id AS OrderID, sales_order_grid.status, sales_order_grid.payment_method AS payment_method, sales_order_grid.base_grand_total AS pmt, sales_order_grid.base_grand_total AS charged, t.code AS tax_code, t.title AS taxrate, t.amount AS taxamount, 
  sales_order_grid.shipping_and_handling AS shipping_invoice, sales_order_grid.grand_total AS total_invoice, customer_address_entity.firstname, customer_address_entity.lastname, 
  customer_address_entity.city, customer_address_entity.region, customer_address_entity.postcode 
  FROM sales_order_grid
  JOIN customer_address_entity on sales_order_grid.customer_id = customer_address_entity.parent_id
  LEFT OUTER JOIN sales_order_tax AS t on sales_order_grid.entity_id = t.order_id
  WHERE sales_order_grid.created_at >= '2018-02-01 00:00:00' AND sales_order_grid.created_at <= '2018-02-05 23:59:59'  
  AND sales_order_grid.status IN('Paid','complete','closed','Processing','on hold')  GROUP BY sales_order_grid.entity_id