sqlselect:连接3个表并对最后一个表进行计数

kuhbmx9i  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(278)

我有3个表,想与它们进行联接。->没问题!我的挑战来了。。。我无法解决:
我要添加的最后一个表是“附件”表。但是我想对这个表进行计数以获得筛选器的行数:where attachments.audit\u id=。。。和附件。问题\u id=。。。
我现在不知道如何在这个查询中使用count方法。
除了这个复杂问题之外,我还有一个关于sql连接的一般问题:在我的代码中,在连接之后使用where。这会影响所有表吗?所以整个查询?或者只使用属性开头的表?
这是我迄今为止的尝试。。。。

$statement = $pdo->prepare("SELECT  
                                    answers.question_id, 
                                    answers.rating, 
                                    questions.id, 
                                    questions.chapter_number, 
                                    questions.custom_question_number, 
                                    questions.question_header_text, 
                                    questions.question, 
                                    questions.critical_question, 
                                    questions.question_information 

                            FROM answers 
                            INNER JOIN questions ON answers.question_id = questions.id 
                            INNER JOIN attachments
                            WHERE questions.questionaire_id = :questionaire_id AND questions.chapter_number = :chapter_number
                            ORDER BY questions.id ASC ");
$statement->execute(array(':questionaire_id' => '1', ':chapter_number' => '2')); 

                            $count = 1;
                            while($row = $statement->fetch()) {
7gs2gvoe

7gs2gvoe1#

我要添加的最后一个表是“附件”表。但是我想对这个表进行计数以获得筛选器的行数:where attachments.audit\u id=。。。和附件。问题\u id=。。。
在另一个查询中执行
除了这个复杂问题之外,我还有一个关于sql连接的一般问题:在我的代码中,在连接之后使用where。这会影响所有表吗?所以整个查询?或者只使用属性开头的表?
第一 join 语句按如下指定的条件组合表 JOIN questions ON answers.question_id = questions.id 那么 where 按条件筛选联接数据的语句
sql指令的执行顺序(mysql):
从,连接
哪里
分组依据

窗口
选择
不同的
联盟
订货人
限制,偏移

相关问题