gpt4 book ai didi

mysql - 显示来自两个外键的列数据

转载 作者:行者123 更新时间:2023-11-30 21:44:02 25 4
gpt4 key购买 nike

我很困惑,想知道如何根据两个不同的外键显示具有两列(城市名称)的表。在这种情况下,我想显示一个制造商城市和一个仓库城市,而它们都需要访问 CityID。

ERD

这是我到目前为止的结果:

SELECT s.StkName, c.CityName AS "Manufacturer City", c.CityName AS 
"Warehouse City", s.SellingPrice FROM
STOCKITEM7364 s
LEFT OUTER JOIN MANUFACTURER7364 m ON m.ManID = s.ManID
LEFT OUTER JOIN CITY7364 c ON c.CityID = m.CityID
LEFT OUTER JOIN WAREHOUSE7364 w ON w.WHID = s.WHID AND w.CityID = c.CityID
WHERE s.StkName LIKE '%n' OR s.StkName LIKE '%e' OR s.StkName LIKE '%o' OR
s.StkName LIKE '%p' OR s.StkName LIKE '%s';

我非常感谢有关 sql 的任何帮助或解释

最佳答案

城市名表需要加入两次,一次是厂家,一次是仓库。试试这个:

SELECT s.StkName, cm.CityName AS "Manufacturer City", cw.CityName AS 
"Warehouse City", s.SellingPrice FROM
STOCKITEM7364 s
LEFT JOIN MANUFACTURER7364 m ON m.ManID = s.ManID
LEFT JOIN CITY7364 cm ON c.CityID = m.CityID
LEFT JOIN WAREHOUSE7364 w ON w.WHID = s.WHID
LEFT JOIN CITY7364 cw ON c.CityID = w.CityID
WHERE s.StkName LIKE '%n' OR s.StkName LIKE '%e' OR s.StkName LIKE '%o' OR
s.StkName LIKE '%p' OR s.StkName LIKE '%s';

请注意 LEFT JOIN 中不需要 OUTER

关于mysql - 显示来自两个外键的列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50302470/

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