内联在同一张桌子上两次 [英] Inner Join twice in the same table

查看:36
本文介绍了内联在同一张桌子上两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

im having an issue with my inner join,these are my tables with FK and 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

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

so i just want to make the result look like this:

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

谢谢

推荐答案

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

List the table twice in the from clause with different aliases.

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

This will give you different cities, one for the manufacturer and one for the depot.

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

这篇关于内联在同一张桌子上两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆