如何在php中连接两个mysql表
我有两个表,我想用jointtwo表进行查询。 如果有pro_pic='NULL',那么我希望在没有NULL pro_pic的情况下得到结果 这是表格: 用户信息如何在php中连接两个mysql表,php,mysql,mysqli,Php,Mysql,Mysqli,我有两个表,我想用jointtwo表进行查询。 如果有pro_pic='NULL',那么我希望在没有NULL pro_pic的情况下得到结果 这是表格: 用户信息 ID user_id full_name country -------------------------------------- 1 Star01 Sagor Us 2 Star02 Rofiq India 3 Star0
ID user_id full_name country
--------------------------------------
1 Star01 Sagor Us
2 Star02 Rofiq India
3 Star03 Karim Aus
专业人士
ID user_id pro_pic
---------------------------------
1 Star01 14523.png
2 Star02 NULL
3 Star03 554513.png
所以我想得到这样的结果
ID user_id full_name country pro_pic
------------------------------------------------------------------
1 Star01 Sagor Us 14523.png
3 Star03 Karim Aus 554513.png
未显示空pro_pic字段
这是我的查询,但我没有得到结果:'(
试试这个
$stmt1 = mysqli_query($con,"SELECT ui.user_id, pp.pro_pic, pp.user_id, ui.full_name, ui.country
FROM user_info ui
INNER JOIN pro_pic pp ON ui.user_id=pp.user_id where pp.pro_pic IS NOT NULL
ORDER BY ui.user_id limit $current, $limit");
编辑:添加了ORDERBY子句
请注意,无论何时使用limit子句,都应该使用ORDERBY子句,因为如果不使用ORDERBY子句,MySQL结果总是返回随机行,请使用此查询
$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
以下是代码的链接:
希望能有所帮助。@pothharapothik请看我更新了我的答案,并添加了Caluse的订单。你得到了什么结果?看,为什么Star02会有争议?
$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";