繁体   English   中英

SQL Select:加入 3 个表并在最后一个表上进行计数

[英]SQL Select: Joining 3 tables and do on the last table an count

我有 3 张桌子,想和他们一起做 JOIN。 -> 没问题。 我的挑战来了..:我无法解决:

我要添加的最后一个表格是表格“附件”。 但我想对此表进行计数以获取过滤器的行数: WHERE attachments.audit_id =... AND attachments.question_id =...

我现在不知道如何在这个查询中使用 count 方法。

除了这个并发症,我对 SQL JOIN 有一个一般性问题:在我的代码中,我在 JOIN 之后使用 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()) {

我要添加的最后一个表格是表格“附件”。 但我想对该表进行计数以获取过滤器的行数: WHERE attachments.audit_id =... AND attachments.question_id =...

只需在另一个查询中执行

除了这个并发症,我对 SQL JOIN 有一个一般性问题:在我的代码中,我在 JOIN 之后使用 WHERE。 这会影响现在所有表,所以整个查询? 还是只有我在属性开头使用的表格?

第一个join语句按指定条件组合表,例如JOIN questions ON answers.question_id = questions.id then where语句按条件过滤连接数据

sql 指令的执行顺序(MySQL):

  1. 从,加入
  2. 在哪里
  3. 通过...分组
  4. 拥有
  5. WINDOW
  6. SELECT
  7. 清楚的
  8. 联盟
  9. 订购方式
  10. 限制,偏移

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM