2015-04-03 21 views
1

首先,感謝您的人花費時間閱讀本文。php如何獲得多個表的列值與條件

現在我有兩個表,和下面的數據是:

表名:表1

+-----------+----------+---------------------------+ 
| tid  | area  | Subject     | 
+-----------+----------+---------------------------+ 
| 1   | US  | The one restaurant  | 
| 2   | US  | Landmark hotel   | 
| 3   | US  | Tholo restaurant   | 
| 4   | CA  | GE bar     | 
+-----------+----------+---------------------------+ 

表名:表2

+--------+---------+---------------------------+---------------+ 
| tid | area | Value      | optionid  | 
+--------+---------+---------------------------+---------------+ 
| 1  | US  | the one restaurant desc | restaurant | 
| 1  | US  | the one rest. contact  | recontact  | 
| 1  | US  | the one rest. address  | readdress  | 
| 2  | US  | landmark hotel desc  | hotel   | 
| 2  | US  | landmark hotel.contact | hocontact  | 
| 2  | US  | landmark hotel.address | hoaddress  | 
| 3  | US  | Tholo restaurant.desc  | restaurant | 
| 3  | US  | Tholo restaurant.contact | recontact  | 
| 3  | US  | Tholo restaurant.address | readdress  | 
| 4  | CA  | GE bar.desc    | bar   | 
| 4  | CA  | GE bar.contact   | bacontact  | 
| 4  | CA  | GE bar.address   | baaddress  | 
+--------+---------+---------------------------+---------------+ 

我想顯示的數據等當用戶查詢區域= US時

|tid | Subject    | description    | contact      | Address    |area| 
+-----+----------------------+-------------------------+-----------------------------+--------------------------+----+ 
| 1 | The one restaurant | the one restaurant desc | the one rest.contact  | the one rest.address  | US | 
| 2 | Landmark hotel  | landmark hotel desc  | landmark hotel contact  | landmark.address   | US | 
| 3 | Tholo restaurant  | Tholo restaurant.desc | Tholo restaurant.contact | Thoro restaurant.address | US | 

現在我只能成功,別名和工會,不能加入主題,我的代碼如下:

$query = mysql_query("SELECT value AS restaurant_description FROM table2 WHERE fid = US AND optionid = restaurant UNION SELECT value AS restaurant_contact FROM table2 WHERE fid = US AND optionid = recontact UNION SELECT value AS restaurant_address FROM table2 WHERE fid = US AND optionid = readdress UNION SELECT value AS hotel FROM table2 WHERE fid = US AND optionid = hotel UNION SELECT value AS hotel_description FROM table2 WHERE fid = US AND optionid = hotel UNION SELECT value AS hotel_contact FROM table2 WHERE fid = US AND optionid = hocontact UNION SELECT value AS hotel_address FROM table2 WHERE fid = US AND optionid = hoaddress UNION SELECT value AS bar_description FROM table2 WHERE fid = US AND optionid = bar UNION SELECT value AS bar_contact FROM table2 WHERE fid = US AND optionid = bacontact UNION SELECT value AS bar_address FROM table2 WHERE fid = US AND optionid = baaddress"); 

    while($row = mysql_fetch_array($query)) { 
     echo $row['hotel_description']; 
     echo $row['hotel_lat']; 
     echo $row['subject']; 
} 

我嘗試下面的代碼,但它不能顯示我需要的數據。

$query = mysql_query("SELECT value AS restaurant_description FROM table2 WHERE fid = US AND optionid = restaurant UNION SELECT value AS restaurant_contact FROM table2 WHERE fid = US AND optionid = recontact UNION SELECT value AS restaurant_address FROM table2 WHERE fid = US AND optionid = readdress UNION SELECT value AS hotel FROM table2 WHERE fid = US AND optionid = hotel UNION SELECT value AS hotel_description FROM table2 WHERE fid = US AND optionid = hotel UNION SELECT value AS hotel_contact FROM table2 WHERE fid = US AND optionid = hocontact UNION SELECT value AS hotel_address FROM table2 WHERE fid = US AND optionid = hoaddress UNION SELECT value AS bar_description FROM table2 WHERE fid = US AND optionid = bar UNION SELECT value AS bar_contact FROM table2 WHERE fid = US AND optionid = bacontact UNION SELECT value AS bar_address FROM table2 WHERE fid = US AND optionid = baaddress UNION SELECT subject FROM table1 WHERE fid = US AND table1.tid = table2.tid"); 

回答

1

那更像創建pivot表。如果您只想要recontactrestaurantreaddress每個值,您可以使用以下技術。

select 
t1.tid, 
t1.subject, 
t2.area, 
max(case when t2.optionid = 'restaurant' then t2.Value end) as `description`, 
max(case when t2.optionid = 'recontact' then t2.Value end) as `contact`, 
max(case when t2.optionid = 'readdress' then t2.Value end) as `Address` 
from table1 t1 
join table2 t2 on t1.tid = t2.tid 
group by t1.tid; 
+0

嗨,感謝您的幫助,但是當我嘗試使用area = US時,它也不能顯示任何數據, 我會通過如下鏈接請求:http://www.example.com/info.php ?areacode = US, 之後,在PHP將有這樣的代碼, $ area = $ _GET ['areacode']; 如何在MySQL查詢中插入$區域? – 2015-04-04 02:45:57

+0

你需要在'group by'子句前面有't2.area = $ area'。 – 2015-04-04 06:19:50

0
$query = mysql_query("SELECT value AS restaurant_description FROM table2 WHERE `fid` ="US" AND `optionid` = "restaurant" UNION SELECT value AS restaurant_contact FROM table2 WHERE `fid` = "US" AND `optionid` = "recontact" UNION SELECT value AS restaurant_address FROM `table2` WHERE `fid` ="US" AND `optionid` = "readdress" UNION SELECT value AS hotel FROM `table2` WHERE `fid` = "US" AND `optionid` = "hotel" UNION SELECT value AS hotel_description FROM `table2` WHERE `fid` = "US" AND `optionid` = "hotel" UNION SELECT value AS hotel_contact FROM `table2` WHERE `fid` = "US" AND `optionid` = "hocontact" UNION SELECT value AS hotel_address FROM `table2` WHERE `fid` = "US" AND `optionid` = "hoaddress" UNION SELECT value AS bar_description FROM `table2` WHERE `fid` = "US" AND `optionid` = "bar" UNION SELECT value AS bar_contact FROM table2 WHERE `fid` = "US" AND `optionid` = bacontact UNION SELECT value AS bar_address FROM `table2` WHERE `fid` ="US" AND `optionid` = "baaddress" UNION SELECT subject FROM `table1` WHERE `fid` = "US" AND `table1`.tid = `table2`.tid"); 

先改變你這樣的查詢和回聲它,如果它表現出了比任何錯誤讓我知道

+0

如果包括 「UNION SELECT主題FROM'table1' WHERE'fid' = 」US「 和'table1'.tid ='table2'.tid」 只會是空白的,如果刪除「UNION SELECT受試者'table1' WHERE'fid' =「US」AND'table1'.tid ='table2'.tid「可以正常工作,但不能顯示主題。 – 2015-04-04 03:02:39

相關問題