单个 SQL 查询中的多个计数

2021-12-30 00:00:00 join sql count mysql

我正在尝试使用以下代码获取 4 个特定部分内的文档数:

I'm trying to get the count of documents within 4 specific sections using the following code:

SELECT
    category.id
    , category.title
    , count(ts1.section_id) AS doc1
    , count(ts2.section_id) AS doc2
    , count(ts3.section_id) AS doc3
    , count(ts4.section_id) AS doc4
FROM
    category 
    LEFT JOIN category_link_section AS ts1
        ON (category.id = ts1.category_id AND ts1.section_id = 1)
    LEFT JOIN category_link_section AS ts2
        ON (category.id = ts2.category_id AND ts2.section_id = 2)
    LEFT JOIN category_link_section AS ts3
        ON (category.id = ts3.category_id AND ts3.section_id = 3)
    LEFT JOIN category_link_section AS ts4
        ON (category.id = ts4.category_id AND ts4.section_id = 4)
GROUP BY category.id, ts1.section_id, ts2.section_id, ts3.section_id, ts4.section_id

表格类别"有一个 id、标题等.表category_link_section"包含 category_id、section_id 和 doc_id 之间的 id 链接.

The table 'category' had an id, title etc. The table 'category_link_section' contains id linkages between category_id, section_id, and doc_id.

如果任何列的计数为 0,则在该列中显示 0.但如果结果不为 0,则显示所有部分结果的乘法结果.所以如果我的 4 个计数列应该返回:1, 2, 0, 3;它实际上会显示 6, 6, 0, 6;

If the count is 0 for any column, it displays 0 in that column. But if the result is not 0 it shows the multiplication result of all the section results. So if my 4 count columns were supposed to return: 1, 2, 0, 3; it would actually show 6, 6, 0, 6;

如果我对每个特定类别使用以下代码,我会得到我想要的结果:

If I use this following code for each specific category I get the results I want:

SELECT
    category.id
    , category.title
    , count(ts1.section_id) AS doc1
FROM
    category 
    LEFT JOIN category_link_section AS ts1
        ON (category.id = ts1.category_id AND ts1.section_id = 1)
GROUP BY category.id, ts1.section_id

但是我每次都需要为每个部分循环访问数据库.

but I then need to cycle through the database each time for each section.

所以我的问题是,我是否需要依次遍历并调用每个部分,在 SQL 之外构建我的表,还是可以在单个查询中完成?

So my question is, do I need to step through and call each section in turn, constructing my table outside the SQL, or can this be done in a single query?

推荐答案

@VoteyDisciple 的 answer 是正确的,但他的查询需要一些改进:

@VoteyDisciple's answer is on the right track, but his query needs some improvements:

SELECT c.id, c.title,
    SUM(ts1.section_id = 1) AS doc1,
    SUM(ts1.section_id = 2) AS doc2,
    SUM(ts1.section_id = 3) AS doc3,
    SUM(ts1.section_id = 4) AS doc4
FROM category AS c
  LEFT JOIN category_link_section AS ts1
    ON (c.id = ts1.category_id)
GROUP BY c.id;

<小时>

说明:

  • IF() 表达式是多余的,因为相等已经返回 1 或 0.
  • ts1.section_id=1 从连接条件中取出,否则您将永远无法获得其他 section_id 值.
  • 仅按 c.id 分组.我假设 OP 只需要每个类别的一行,以及相应类别的每个 section_id 值的计数的列.如果查询按 c.id, ts1.section_id 分组,则每个类别最多有四行.
  • 在选择列表中移动逗号.浮动在行首的逗号看起来很难看.;-)
  • The IF() expressions are redundant because equality already returns 1 or 0.
  • Take the ts1.section_id=1 out of the join condition, or you'll never get the other section_id values.
  • Group by c.id only. I assume the OP only wants one row per category, and columns for counts of each section_id value for the respective category. If the query grouped by c.id, ts1.section_id, then there'd be up to four rows per category.
  • Move the commas in the select-list. Commas floating at the start of the line look ugly. ;-)

相关文章