简体   繁体   English

SQL如何为每个字段计数表

[英]SQL How to Count table for each fields

I have 3 tables which is: 我有3张桌子,它们是:

Courses 课程

  • courses_id courses_id
  • name 名称

QnAs QnAs

  • qna_id qna_id
  • student_id 学生卡
  • courses_id courses_id
  • name 名称
  • question

Students 学生们

  • student_id 学生卡
  • name 名称

Now I'm trying to count how many qna's there are for each courses. 现在,我试图计算每个课程有多少个qna。 How do i make the query? 如何进行查询?

I've tried doing this : 我尝试这样做:

SELECT (SELECT COUNT(qna_id) AS Expr1
              FROM    QnAs) AS Count
FROM   QnAs AS QnAs_1 CROSS JOIN
             Courses
GROUP BY Courses.courses_id

It does counts how many QnA's there are but not for each Courses 它确实会计算有多少QnA,但不是每个课程

The output i got is each Courses names and QnAs count number but what i want is the QnA's number for each of the Courses 我得到的输出是每个课程的名称和QnA计数编号,但是我想要的是每个课程的QnA编号

Why not just use GROUP BY ? 为什么不只使用GROUP BY

SELECT q.courses_id, COUNT(qna_id) as cnt
FROM QnAs q
GROUP BY q.courses_id;

It seems you merely want to aggregate QNAs by course ID: 看来您只想按课程ID汇总QNA:

select courses_id, count(*)
from qnas
group by courses_id
order by courses_id;

Along with the course names: 连同课程名称:

select c.course_id, c.name, coalesce(q.cnt, 0) as qna_count
from courses c
left join
(
  select courses_id, count(*) as cnt
  from qnas
  group by courses_id
) q on q.course_id = c.course_id
order by c.course_id;

This is not an answer, but just an explanation what your query does. 这不是答案,而只是解释查询的作用。

In your own query you first cross join all QnAs with all courses for no apparent reason, thus getting all possible combinations. 在您自己的查询中,您首先将所有QnA与所有课程交叉交叉(没有明显的原因),从而获得了所有可能的组合。 So with two courses, each with three QNAs (that makes six QNAs in total), you'd construct 2 x 6 = 12 rows. 因此,如果有两门课程,每门课程都有三个QNA(总共六个QNA),则您将构造2 x 6 = 12行。

For each of these rows you select the total number of rows in the QNA table, which is six in above example. 对于这些行中的每一行,请选择QNA表中的总行数,在上面的示例中为6。 So you'd select 12 rows, all showing the number 6. 因此,您将选择12行,全部显示数字6。

But then you group by course ID, thus ending up with two rows only in my example. 但是,然后您按课程ID进行分组,因此在我的示例中仅以两行结尾。 You should apply an aggregate function on your subquery, eg MAX or SUM , but you don't, which makes your query invalid (because you are dealing with many rows, but treat this as if it were a single value). 您应该在子查询上应用聚合函数,例如MAXSUM ,但不要这样做,这会使查询无效(因为您要处理的行很多,但应将其视为单个值)。 MySQL however silently applies ANY_VALUE , so your query becomes: 但是,MySQL默默地应用ANY_VALUE ,因此您的查询变为:

SELECT
  ANY_VALUE( (SELECT COUNT(*) FROM QnAs) ) AS Count
FROM QnAs AS QnAs_1 
CROSS JOIN Courses
GROUP BY Courses.courses_id;

I hope this explanation helps you understand how joins and aggregation work. 我希望这种解释可以帮助您了解联接和聚合的工作方式。 You may want to set ONLY_FULL_GROUP_BY mode ( https://dev.mysql.com/doc/... ) in order to have MySQL report the syntax error instead of silently "fixing" the query by applying ANY_VALUE . 您可能希望设置ONLY_FULL_GROUP_BY模式( https://dev.mysql.com/doc / ... ),以便让MySQL报告语法错误,而不是通过应用ANY_VALUE静默“修复”查询。

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

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