如何从使用另一个表中的值的表中选择数据

时间:2017-03-12 15:35:59

标签: php mysql mysqli

我想使用另一个名为tbl_users的表中的值和名为mergeing的列从名为donator_1的表中选择数据。

我尝试了以下内容:

$result = $DBcon->query("SELECT tbl_users.username, tbl_users.email, tbl_users.Phone_number FROM tbl_users,mergeing WHERE mergeing.donator_1= tbl_users.user_id AND mergeing._id = 6");
while($row = $result->fetch_array()) {
  echo '<b>' . $l['user_id'] .'</b><b>' . $l['username'] . '</b>';
}  

1 个答案:

答案 0 :(得分:1)

您可以尝试嵌套查询:

SELECT username, email, Phone_number
FROM tbl_users
WHERE user_id = (SELECT donator_1 FROM mergeing WHERE _id = 6 )

或内部联接:

SELECT username, email, Phone_number
FROM tbl_users
JOIN mergeing ON tbl_users.user_id = mergeing.donator_1
WHERE mergeing._id = 6