我有两个表,一个叫'user',另一个叫'user_info'。PHP MySQL从2个不同的表中选择*并显示合并的数据
user;
userid | username
------------------------------
1 | mary
2 | john
3 | liam
user_info;
userid | desc
----------------------------------------------------
1 | hello, my name is mary i am 26
2 | message me if you need any help
3 | please leave me alone
我想从'user_info'表中显示来自'user'表的信息基于userID的相应描述。所以它会出现像:
Name: John
UserID: 2
Description: message me if you need any help
这是我的代码;
//Query the database
$resultSet = $mysqli->query("SELECT * FROM user,user_info");
//Count the returned rows
if ($resultSet->num_rows != 0) {
//Turn the results into an ArrayAccess
while($rows = $resultSet->fetch_assoc())
{
$uid = $rows['UserID'];
$username = $rows['username'];
$desc = $rows['desc'];
echo "
Name: $username
User ID: $uid
Description: $desc
";
}
目前它返回尽可能多的不同组合,因为它可以;像这样:
Name:Mary
UserID:1
Description:hello, my name is mary i am 26
--------------------------------------------
Name:John
UserID:1
Description:message me if you need any help
--------------------------------------------
Name:Liam
UserID:1
Description:please leave me alone
--------------------------------------------
Name:Mary
UserID:2
Description:hello, my name is mary i am 26
--------------------------------------------
Name:John
UserID:2
Description:message me if you need any help
--------------------------------------------
ETC...
我怎样才能让这个只返回一个基于用户ID(仅一次),以便显示所有与他们正确的用户名,标识和说明用户正确的对应数据?
我不知道怎么回事,解释这一点,如果有人想我进一步阐述我没有问题再次尝试,
2017-06-20
John107
+0
使用加入,SELECT * FROM X左加入y其中X。 user_id = y.userid –