我有这两个表:
表一:用户
id name home_location job_location
1 Jack 40 5
2 Rocky 50 4
3 Tom 24 9
表二:面积
area_id area_name
5 Bukit Batok
4 Bukit Panjang
9 Boon Lay
40 Pioneer
50 Choa Chu Kang
24 Clementi
我想要这样的结果:
ID name home_location job_location
1 Jack Pioneer Bukit Batok
2 Rocky Choa Chu Kang Bukit Panjang
3 Tom Clementi Boon Lay
由于我在SQL查询中不好,所以如何编写select查询.
任何想法或建议?谢谢.
解决方法:
试试吧
SELECT id as ID,
name,
area_1.area_name as home_location,
area_2.area_name as job_location,
area_1.area_id as home_location_id,
area_2.area_id as job_location_id
FROM user
INNER JOIN
area AS area_1
ON area_1.area_id = user.home_location
INNER JOIN
area AS area_2
ON area_2.area_id = user.job_location
并尝试避免MysqL_ *语句,因为整个ext / MysqL PHP扩展提供了所有以前缀MysqL_ *命名的函数,从PHP v5.5.0开始正式弃用,将来会被删除.
还有另外两个MysqL扩展你可以更好地使用:MysqLi和PDO_MysqL,其中任何一个都可以用来代替ext / MysqL.