我有一个tableBusiness。有列IDBuildingID

BuildingID也指向tableBusiness。建筑物是企业。

现在我要创建行,其中每一行包含

ID, BusinessTitle, BuildingTitle定义为BusinessTitleID相同的业务的BuildingID

我该怎么办?

如果不是自我参照,我知道我可以做到

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


在这里,Country实际上是表tablecity中的一列。

但是现在我要BuildingTitle不在任何地方的列。它应该在TitleID相同的业务中使用字段BuildingID

我该怎么拔呢?

我用了

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


除“建筑标题”列仅是公司的标题外,其他方法均能正常工作。我希望这成为公司所在建筑物的标题。

那我做

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

现在我该怎么办?

最佳答案

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


您应该使用Join联接表

10-08 07:17