需要获取2个字段的字段值

时间:2018-02-04 02:41:41

标签: php mysql

我有这3个数据库表

CityTable (List of Cities)
--------
CityCode
CityName


Advertiser (Profile of Advertisers)
----------
AdvertiserCode
AdvertiserName
CityCode


Ads (Transaction Table)
--------
AdsId
AdsTitle
AdsDesc
CityCode
AdvertiserCode

WHERE

Ads.CityCode is Where the Ads is being offered
Advertiser.CityCode is Address of the Advertiser

使用此查询,我只能获取[Ads]

的城市名称
SELECT Ads.AdsTitle, Ads.AdsDesc, AdsCity, CityTable.CityName AS AdsCityName
FROM Ads
LEFT JOIN CityTable ON CityTable.CityCode = Ads.CityCode
LEFT JOIN Advertiser ON Advertiser.AdvertiserCode = Ads.AdvertiserCode

但我还需要获得广告商的[CityName]

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT Ads.AdsTitle, Ads.AdsDesc, AdsCity, CityTable.CityName AS AdsCityName, AdvCity.CityName AS AdvertiserCity
FROM Ads
LEFT JOIN CityTable ON CityTable.CityCode = Ads.CityCode
LEFT JOIN Advertiser ON Advertiser.AdvertiserCode = Ads.AdvertiserCode
LEFT JOIN CityTable as AdvCity ON AdvCity.CityCode = Advertiser.CityCode