简体   繁体   English

如何基于另一个表的ID从表中获取行数

[英]How to get number of rows from table based on ID from another table

So I have an index.php page that spits out data from two tables on my database. 所以我有一个index.php页面,它从数据库的两个表中吐出数据。 The first table, questions contains one column for my QuestionID and looks like this: 在第一个表中, questions包含我的QuestionID的一列,如下所示:

qid
----
 1
 2
 3

The second table called answers contains two columns, one for my AnswersID and the other which links to my QuestionsID on the questions table, and it looks like this: 第二个表称为answers包含两列,一列用于我的AnswersID,另一列链接到questions表上的我的QuestionsID,看起来像这样:

aid | aqid
----|-----
 1  |  3
 2  |  1
 3  |  1

So on my index.php page, I basically want to echo out every qid row from the questions table and beside it the total number of answers ( aid rows from the answers table) corresponding to each QuestionID . 因此,在我的index.php页面上,我基本上想在questions表中回显每个qid行,并在其旁边回显与每个QuestionID相对应answers 总数 (从answers表开始的aid行)。 To put simply, the result I want achieved on my index.php page is this: 简而言之,我想要在index.php页面上实现的结果是:

QuestionID: 1 / Answers: 2
QuestionID: 2 / Answers: 0
QuestionID: 3 / Answers: 1

But based on my current code I am having trouble achieving this result. 但是根据我当前的代码,我很难达到这个结果。 It keeps echoing out the wrong number of rows for each table. 它不断回显每个表的错误行数。 This is the unwanted result I am getting: 这是我得到的不必要的结果:

QuestionID: 1 / Answers: 3
QuestionID: 2 / Answers: 3
QuestionID: 3 / Answers: 3

So how would I fix this to achieve the correct result? 那么,如何解决此问题以获得正确的结果? This is my current code: 这是我当前的代码:

<?php 
$sql = "
SELECT questions.*, GROUP_CONCAT(answers.aqid) AS aqid
FROM questions 
LEFT JOIN answers
ON questions.qid=answers.aqid
GROUP 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 } ?>

Thank you for your help. 谢谢您的帮助。

group_concat() returns a concatenated string. group_concat()返回一个串联的字符串。 You might be looking for count() . 您可能正在寻找count()

Also, count() returns a row count. 同样, count()返回行数。
There doesn't seem to be a need for $row_cnt or the foreach . 似乎不需要$row_cntforeach

Here's an example: 这是一个例子:

$sql = "
    SELECT
        q.`qid`,
        COUNT(a.`aqid`) AS `answerCount`
    FROM `questions` q
    LEFT JOIN `answers` a
        ON a.`aqid` = q.`qid`
    GROUP BY q.`qid`
    ";

$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
    ?>
    <div>
      QuestionID: <?php echo $row["qid"]; ?>
      Answers: <?php echo $row["answerCount"]; ?>
    <div>
    <?php
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从同一表获取基于parent_id的行以与其他行相对应 - How do I get rows to correspond to other rows based on a parent_id , all from the same table 如何基于CodeIgniter中另一个表的ID从数据库表中获取结果 - How do i get results from a database table based on the id of another table in CodeIgniter mysql-根据ID和DATE从一个表中获取一行,如果不显示空值,则在另一表中获取多行 - mysql - taking one row from one table based on ID and DATE and get multiple rows in another table if not show null values Laravel从另一个表中基于user_id获取用户名 - Laravel Get Username based on user_id from another table Laravel 根据id从另一个表中获取值 - Laravel get value from another table based on id 根据另一个表的匹配结果从一个表中选择行 - selecting rows from a table based on the matching results from another table MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table 从一个表中选择行,其中基于第二个表ID的另一个第二个表中的ID显示结果的顶部以及下一个结果,因为它是+ Mysql - Select rows from a table where id in another second table based on second table ids shows results top as well as next results as it is + Mysql 从表中获取最后一个 ID 号 - Get last ID number from table 如何从mysql表中获取总行数? - how to get total number of rows from mysql table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM