gpt4 book ai didi

mysql - 同一张表内两次内连接

转载 作者:行者123 更新时间:2023-11-29 12:20:00 25 4
gpt4 key购买 nike

我的内部连接有问题,这些是我的带有 FK 和 PK 的表

TABLE CITY
city_id (PK)
city_name
state

TABLE DEPOT
dep_id (PK)
capacity
city_id (FK) references CiTy

TABLE MANUFACTURER
manu_id (PK)
manu_name
city_id (FK) references city

所以我只想让结果看起来像这样:

DEPOT_CITY_name(referencese from city_id), MANUFACTURER_CITY_name(references from city_id)

谢谢

最佳答案

在 from 子句中使用不同的别名列出该表两次。

这将为您提供不同的城市,一个用于制造商,一个用于仓库。

SELECT dc.city_name AS depot_city
, mc.city_name AS manufacturer_city
FROM DEPOT AS d
JOIN CITY AS dc
ON dc.city_id = d.city_id
JOIN MANUFACTURER AS m
ON m.some_column = d.some_column -- or however these tables relate
JOIN CITY AS mc
ON mc.city_id = m.city_id

关于mysql - 同一张表内两次内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29166989/

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