首先,感谢您的家伙花时间阅读本文。
现在我有两个表,数据如下:
表名:table1
+-----------+----------+---------------------------+
| tid | area | Subject |
+-----------+----------+---------------------------+
| 1 | US | The one restaurant |
| 2 | US | Landmark hotel |
| 3 | US | Tholo restaurant |
| 4 | CA | GE bar |
+-----------+----------+---------------------------+
表名:table2
+--------+---------+---------------------------+---------------+
| 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");
答案 0 :(得分:1)
这更像是创建pivot
表。如果您只需要recontact
,restaurant
和readdress
值,则可以使用以下技术。
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;
答案 1 :(得分: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");
首先像这样更改您的查询并在显示任何错误时回复它,而不是让我知道