gpt4 book ai didi

mysql - 连接多个表时遇到问题

转载 作者:行者123 更新时间:2023-11-29 19:21:16 25 4
gpt4 key购买 nike

我正在尝试为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

最佳答案

联合语法不正确,在每个选择部分后分配表名称

    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

关于mysql - 连接多个表时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com