我正在尝试使用以下代码来获取4个特定部分中的文档数:
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
表“ category”具有ID,标题等。表“ category_link_section”包含category_id,section_id和doc_id之间的ID链接。
如果任何列的计数为0,则在该列中显示0。但是,如果结果不为0,则显示所有分段结果的相乘结果。因此,如果我的4个计数列应该返回:1、2、0、3;它实际上将显示6、6、0、6;
如果我对每个特定类别使用以下代码,则会得到想要的结果:
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
但是随后我需要为每个部分每次都遍历数据库。
所以我的问题是,我是否需要逐步执行并依次调用每个部分,在SQL外部构造表,还是可以在单个查询中完成?
@VoteyDisciple的答案是正确的,但是他的查询需要一些改进:
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()
ts1.section_id=1
section_id
c.id
c.id, ts1.section_id