2011-11-07 52 views
0

我有一個tableBusiness。有列IDBuildingID在自參考表中顯示相同的列兩次

BuildingID也指向tableBusiness。建築是一項生意。

現在我想創建行,其中每行包含

ID, BusinessTitle, BuildingTitle定義爲,其ID業務的BusinessTitleBuildingID

我怎麼會做這樣的一樣嗎?

如果不是自我指涉我知道我可以做

$query="Select *, Country FROM `tablebusiness` As TB, `tablecity` As TC 
     WHERE (".$IDWhereFinal.") AND TB.City=TC.City"; 

這裏,Country實際上是表tablecity列。

但是現在我想BuildingTitle這不是任何地方的列。它應該在IDBuildingID相同的業務中使用Title

我該如何解決這個問題?

我用

SELECT * , Country, (

SELECT Title 
FROM `tablebusiness` AS TBuild 
WHERE TBuild.ID = TB.Building 
) AS Coba 
FROM `tablebusiness` AS TB, `tablecity` AS TC 
WHERE TB.City = TC.City 

看起來工作得很好,但那種異樣的

嘗試另一種答案。

Select *, Country, TB.Title as BuildingTitle FROM `tablebusiness` As TB, `tablecity` As TC 
     WHERE TB.ID<30 AND TB.City=TC.City 

除了專欄BuildingTitle只是業務的標題以外,工作正常。我想,要在建築物的業務是在標題。

然後我做

SELECT * , Country, Building.Title AS BuildingTitle 
FROM `tablebusiness` AS TB, `tablebusiness` AS Building, `tablecity` AS TC 
WHERE TB.ID <30 
AND TB.City = TC.City 
AND TB.building = Building.id 
LIMIT 0 , 30 

我只在建築物內,企業

ID Email InBuildingAddress Price Street Title Website  Zip  Rating Star  Rating Weight Latitude Longitude Building City OpeningHour  TimeStamp ID Email InBuildingAddress Price Street Title Website  Zip  Rating Star  Rating Weight Latitude Longitude Building City OpeningHour  TimeStamp City Country  Country  BuildingTitle 
6 NULL Unit No. 11 Lt. 6 Food Court NULL Jl. Arteri S. Parman Citra NULL NULL 0 0 -6.168758 106.787377 200004 Jakarta  Everyday, 10:00 - 22:00  2011-11-06 22:30:37  200004 [email protected] Lt. P-1 Unit 1 & 52-53 NULL Jl. Letjen S. Parman No. 31  Mall Ciputra NULL 11470 4.33333333333333 3 -6.16852955233882 106.787015629421 0 Jakarta Barat NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Mall Ciputra 
7 NULL Lt. 2 No 40  NULL Jl. K.H. Noer Ali Eaton Bakery and Restaurant  NULL NULL 0 0 -6.248135 106.991926 200005 Jakarta  Monday - Sunday 10:00 - 22:00 2011-11-06 22:30:37  200005 NULL NULL NULL Jl. K.H. Noer Ali Metropolitan Mall Bekasi NULL NULL 0 0 -6.248135 106.991926 0 Jakarta  NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Metropolitan Mall Bekasi 
8 NULL Lt. GF NULL Jl. Pluit Indah Raya Deli Manjoo  NULL NULL 0 0 -6.116341 106.789556 200006 Jakarta  Mon - Sun 10:00 - 22:00  2011-11-06 22:30:37  200006 NULL NULL NULL Jl. Pluit Indah Raya Pluit Village Mall NULL 14450 0 1 -6.11636729665532 106.78957198066  0 Jakarta Utara NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Pluit Village Mall 
9 NULL #18  NULL Jl. Boulevard Palem Raya No. 38  Restaurant India NULL NULL 0 0 -6.22868 106.60923 200007 Jakarta  Everyday, 10:00 - 00:00  2011-11-06 22:30:37  200007 NULL NULL NULL Jl. Boulevard Palem Raya No. 38 lippo Karawaci 120... Benton Junction  NULL 15811 0 0 -6.22827750567979 106.608705787526 0 Tangerang NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Benton Junction 
14 NULL Ground Floor NULL Jl. Raya Boulevard Kelapa Gading Blok M  Mochi-Mochi  NULL NULL 5 1 -6.157515125 106.9084265  200009 Jakarta  Mon - Sun 10:00 - 22:00  2011-11-06 22:30:37  200009 [email protected]  Unit 159 NULL Jl. Bulevar Kelapa Gading Blok M Kelapa Gading Per... Mall Kelapa Gading 2 NULL 14240 1.75 4 -6.15783092569102 106.907815359612 0 Jakarta Utara NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Mall Kelapa Gading 2 
16 NULL NULL NULL Jl. HR. Rasuna Said Lt. Dasar Rumah Makan Aroma Sulawesi NULL NULL 0 0 -6.2210955 106.8329755  200010 Jakarta  Everyday, 10:00 - 22:00  2011-11-06 22:30:37  200010 NULL NULL NULL Jl. HR. Rasuna Said Lt. Dasar Mall Pasar Festival  NULL 12960 1.5  2 -6.22111994514218 106.83291640369  0 Jakarta Selatan  NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Mall Pasar Festival 
17 NULL Lt. 6 Food Court 119 A NULL Jl. Asia Afrika Pintu 9  Alberki Khas Sulawesi NULL NULL 0 0 -6.228376 106.798677 200011 Jakarta  Everyday, 09:00 - 20:00  2011-11-06 22:30:37  200011 [email protected]  NULL NULL Jl. Asia Afrika Pintu 9  STC Senayan  NULL 10270 0.5  2 -6.22839746826785 106.798676515068 0 Jakarta Pusat NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia STC Senayan 
20 NULL Blok Kt Unit 34  NULL Jl. Maleo Raya (Bintaro Sektor 9)  Nasi Kebuli Haji Ali NULL 12330 0 0 -6.27829125  106.72197075 200259 Jakarta Selatan  Everyday, 16:00 - 00:00  2011-11-06 22:30:37  200259 NULL NULL NULL Jl. Maleo Raya (Bintaro Sektor 9)  Bintaro Trade Centre NULL NULL 0 0 -6.27829125  106.72197075 0 Tangerang Selatan NULL 2011-11-06 22:29:28  Jakarta Selatan  Indonesia Indonesia Bintaro Trade Centre 
21 NULL Food Court Floor 5, No 31 NULL Jl.Letjend Suprapto  Khas Sulawesi NULL NULL 0 0 -6.169621625 106.867814 200013 Jakarta  Everyday 11:00 - 18:00 2011-11-06 22:30:37  200013 NULL NULL NULL Jl. Let. Jend Suprapto Kav. 62 No. 1 Grand Cempaka Jakarta Hotel  NULL 10640 0 0 -6.17011293789604 106.868159676677 0 Jakarta Pusat NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Grand Cempaka Jakarta Hotel 
22 NULL Food Court Floor 5, No 86 NULL Jl.Letjend Suprapto  Kwetiau Sapi 88 Pontianak NULL NULL 0 0 -6.169621625 106.867814 200013 Jakarta  Everyday 10:00 - 20:00 2011-11-06 22:30:37  200013 NULL NULL NULL Jl. Let. Jend Suprapto Kav. 62 No. 1 Grand Cempaka Jakarta Hotel  NULL 10640 0 0 -6.17011293789604 106.868159676677 0 Jakarta Pusat NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Grand Cempaka Jakarta Hotel 
23 NULL Lt. 2 Blok A 2 #19 - 21  NULL Jl. Boulevard Artha Gading Selatan No. 1 Toreore Chicken & Joy NULL NULL 0 0 -6.14630575  106.89517725 200014 Jakarta  Mon - Sun 10:00 - 22:00  2011-11-06 22:30:37  200014 NULL NULL NULL Jl. Boulevard Artha Gading Selatan No. 1 Rukan Artha Gading Niaga NULL 14250 2.33333333333333 3 -6.14615740918793 106.89470094061  0 Jakarta Utara NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Rukan Artha Gading Niaga 
24 NULL Food Court Floor 5, No 57 NULL Jl.Letjend Suprapto  Ou Kie NULL NULL 0 0 -6.169621625 106.867814 200013 Jakarta  Everyday 11:00 - 20:00 2011-11-06 22:30:37  200013 NULL NULL NULL Jl. Let. Jend Suprapto Kav. 62 No. 1 Grand Cempaka Jakarta Hotel  NULL 10640 0 0 -6.17011293789604 106.868159676677 0 Jakarta Pusat NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Grand Cempaka Jakarta Hotel 
25 NULL Lt. Pasar Senggol Blok Exb Unit 03 NULL Jl. Letjen S. Parman No. 31  Bakmi Bangka NULL 11470 0 0 -6.168758 106.787377 200004 Jakarta Barat Everyday, 10:00 - 22:00  2011-11-06 22:30:37  200004 [email protected] Lt. P-1 Unit 1 & 52-53 NULL Jl. Letjen S. Parman No. 31  Mall Ciputra NULL 11470 4.33333333333333 3 -6.16852955233882 106.787015629421 0 Jakarta Barat NULL 2011-11-06 22:29:28  Jakarta Barat Indonesia Indonesia Mall Ciputra 
27 NULL Food Court Floor 5, No 98 NULL Jl.Letjend Suprapto  Seroja Baru  NULL NULL 0 0 -6.169621625 106.867814 200013 Jakarta  Everyday 10:00 - 19:30 2011-11-06 22:30:37  200013 NULL NULL NULL Jl. Let. Jend Suprapto Kav. 62 No. 1 Grand Cempaka Jakarta Hotel  NULL 10640 0 0 -6.17011293789604 106.868159676677 0 Jakarta Pusat NULL 2011-11-06 22:29:28  Jakarta  Indonesia Indonesia Grand Cempaka Jakarta Hotel 
28 NULL 4th Fl Unit 57 NULL Jl. Jend. Sudirman Kav. 52 - 53 sudirman Central B... Secret Recipe NULL 12190 0 0 -6.226948 106.8133435  200015 Jakarta Selatan  Mon - Sun 10:00 - 22:00  2011-11-06 22:30:37  200015 NULL Lt. Ground Unit 99 NULL Jl. Jend. Sudirman Kav. 52 - 53 sudirman Central B... Pacific Place Mall NULL 12190 4.33333333333333 3 -6.22628017797778 106.812480471955 0 Jakarta Selatan  NULL 2011-11-06 22:29:28  Jakarta Selatan  Indonesia Indonesia Pacific Place Mall 

說我希望所有的企業無論建築物是否在建築物內。如果建築物是空的,我希望BuildingTitle包含NULL。如果建築物不是空的,我希望BuildingTitle列包含建築物的標題。我該怎麼做?

如果業務位於建築物中,則BuildingTitle列應包含建築物的名稱。如果建築物是空的BuildingTitle應爲NULL

現在我該怎麼辦?

回答

1
​​

你應該使用Join

+0

目前尚不清楚聯接表。 TableBusiness被使用兩次。一旦作爲業務和另一個作爲建築物表。所以你只用了一次。爲什麼? –

+0

答案不起作用。 –

相關問題