无法加入多个表格

时间:2017-02-24 07:06:06

标签: mysql

我试图为invoice_line_items设置一个表,该表链接到一个库存表,根据库存类型,需要将其连接到各个表以获取额外的字段。

invoice_line_items < inventory_id> JOIN inventory < type = m> JOIN inv_extra_m / < type = d> JOIN inv_extra_d

SELECT DISTINCT l.invoice_line_number
FROM invoice_line_items l
LEFT JOIN inventory i
ON i.inventory_id = l.inventory_id
JOIN (
SELECT i.*
FROM inventory i
JOIN inv_extra_m m ON i.inventory_id = m.inventory_id 
WHERE (i.type = 'a' OR i.type = 'b'))
UNION
SELECT i.*
FROM inventory i
JOIN inv_extra_d d ON i.inventory_id = d.inventory_id 
WHERE i.type = 'd'
) ie ON ie.inventory_id = i.inventory_id 
WHERE l.invoice_id = 1

1 个答案:

答案 0 :(得分:0)

你有一个不正确的联合sintax在每个选择部分之后指定一个表名

    SELECT DISTINCT l.invoice_line_number
    FROM invoice_line_items l
    LEFT JOIN inventory i
    ON i.inventory_id = l.inventory_id
    JOIN (
      SELECT i.*
      FROM inventory i
      JOIN inv_extra_m m ON i.inventory_id = m.inventory_id 
      WHERE (i.type = 'a' OR i.type = 'b')
      UNION
      SELECT i.*
      FROM inventory i
      JOIN inv_extra_d d ON i.inventory_id = d.inventory_id 
      WHERE i.type = 'd' 
      ) ie ON ie.inventory_id = i.inventory_id 
    WHERE l.invoice_id = 1