我有以下表格
评论表:
id user_id question_id comment
1 2 3 hii
用户表:
id u_name user_image
2 naveen a.jpg
当选择question_id = 3时,如何打印这样的值:
user_image u_name comment
a.jpg naveen hii <br>
sql查询:
$sql = "SELECT u.user_image, u.name, c.comment
FROM users u INNER JOIN comment c
ON u.id = c.user_id
WHERE c.question_id = 2";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($rows = mysqli_fetch_assoc($result)) {
echo '<td class="small">'.$rows['name'].'</td>';
echo '<td class="small">'.$rows['user_image'].'</td>';
echo '<td class="small">'.$rows['comment'].'</td>';
}
}
else {
echo "0 results";
}
答案 0 :(得分:0)
使用INNER JOIN
:
SELECT u.user_image, u.u_name, c.comment
FROM users u INNER JOIN comments c
ON u.id = c.user_id
WHERE c.question_id = 3