从预定我需要两个连接到国家,两个连接到州和两个连接到城市 .
My Tables
Schedule
Destination Country ID
Destination City ID
Destination State ID
Current Country ID
Current City ID
Current State ID
-------------------
Country
ID
Country
-------------------
State
ID
Statename
Countryid
-------------------
City
ID
City
Stateid
Countryid
我需要从计划表中读取数据并使用国家,州和城市名称列出它,这些名称存储在国家,州和城市表中,并通过ID字段链接 . 我有第一个连接到每个表工作,但当我尝试第二个查询失败说“ Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
” .
什么有用......
$query="SELECT schedule.username, schedule.ret, country.country, state.statename, city.city, schedule.business, schedule.pleasure
FROM schedule
JOIN country
ON schedule.dest_country=country.id
JOIN state
ON schedule.dest_state=state.id
JOIN city
ON schedule.dest_city=city.id";
什么不......
$query="SELECT schedule.username, schedule.ret, country.country, state.statename, city.city, schedule.business, schedule.pleasure, country.country
FROM schedule
JOIN country
ON schedule.dest_country=country.id
JOIN state
ON schedule.dest_state=state.id
JOIN city
ON schedule.dest_city=city.id
JOIN country
ON schedule.cur_country=country.id";
谢谢
2 回答
在连接表上使用别名来分隔重复的表连接:
要澄清结果集,您可以命名结果列:
试试这个SQL兄弟!