所以我有一個 index.php 頁面,它從我的數據庫中的兩個表中吐出數據。第一個表questions包含我的 QuestionID 的一列,如下所示:qid---- 1 2 3調用的第二個表answers包含兩列,一列用于我的 AnswersID,另一列用于鏈接到我在questions表中的QuestionsID ,它看起來像這樣:aid | aqid----|----- 1 | 3 2 | 1 3 | 1所以在我的 index.php 頁面上,我基本上想qid從questions表中回顯出每一行,旁邊是與每個 QuestionID 對應的答案總數(表中的aid行answers)。簡單地說,我想要在我的 index.php 頁面上實現的結果是這樣的:QuestionID: 1 / Answers: 2QuestionID: 2 / Answers: 0QuestionID: 3 / Answers: 1但是根據我當前的代碼,我無法實現這個結果。它不斷為每個表回顯錯誤的行數。這是我得到的不需要的結果:QuestionID: 1 / Answers: 3QuestionID: 2 / Answers: 3QuestionID: 3 / Answers: 3那么我將如何解決這個問題以獲得正確的結果?這是我當前的代碼:<?php $sql = "SELECT questions.*, GROUP_CONCAT(answers.aqid) AS aqidFROM questions LEFT JOIN answersON questions.qid=answers.aqidGROUP BY questions.qid";$result = $conn->query($sql);while($row = $result->fetch_assoc()) { $qid = $row["qid"]; $aqid = $row["aqid"]; $row_cnt = $result->num_rows;?><div> QuestionID: <?php echo $qid; ?> <?php foreach($answers as $answer): ?> Answers: <?php echo $row_cnt; ?> <?php endforeach; ?></div><?php } ?>感謝您的幫助。
如何根據另一個表的ID從表中獲取行數
慕田峪7331174
2021-10-22 16:56:05