我在php中有以下代码
// Get all the questions
$query = mysqli_query($con, "SELECT * FROM da_questions INNER JOIN da_users ORDER BY da_questions.question_id DESC");
$allquestions = array();
while($row = mysqli_fetch_array($query))
{
$allquestions[] = $row;
}
如何使user_id
中的table da_questions
等于user_id
中的da_users
,以便在执行以下操作时,我会获得正确用户ID的正确用户名。
<?php foreach($allquestions as $question): ?>
<span><b><?php echo $question['question_title']; ?><span class="pull-right"><?php echo $question['username']; ?></span></b></span>
<hr style="margin-top:0px;" />
<p><?php echo $question['question_text']; ?></p>
<?php endforeach; ?>
答案 0 :(得分:1)
您的加入中需要ON
条件
SELECT *
FROM da_questions
INNER JOIN da_users ON da_users.user_id = da_questions.user_id
ORDER BY da_questions.question_id DESC