添加where子句后,计数查询停止正常工作

时间:2012-01-21 02:10:27

标签: mysql sql

我有3个表:问题,类别和problem_categories的连接表

我正在尝试获取并计算与问题相关的所有类别,并计算与每个问题相关的问题数量。

这是我的查询,它没有返回正确的结果:

SELECT 
  category_name , 
  categories.category_id , 
  problems.problem_id, 
  COUNT(problems.problem_id) as num_problems 
FROM 
  problem_categories 
left JOIN 
  categories 
on 
  problem_categories.category_id = categories.category_id 
left join 
  problems 
on
  problem_categories.problem_id = problems.problem_id 
WHERE 
  problem_categories.problem_id = 266
GROUP BY 
  problems.problem_id , category_name

但是当我取出where子句时,它会返回所有类别和正确的问题计数。但诀窍是我需要查询特定的problem_id

有什么方法可以调整此查询以使其正确吗?我在这里做错了什么?

谢谢!

4 个答案:

答案 0 :(得分:1)

SELECT p1.problem_id, problem_categories.category_id , category_name , 
    COUNT(*) as num_problems 
FROM problems p1
LEFT JOIN problem_categories  on
   p1.problem_id = problem_categories.problem_id 
LEFT JOIN categories on 
   problem_categories.category_id = categories.category_id 
LEFT JOIN problems p2 ON
   p2.problem_id = problem_categories.problem_id 
WHERE p1.problem_id = 266
GROUP BY categories.category_id,p2.problem_id
ORDER BY categories.category_id

多数民众赞成

答案 1 :(得分:1)

你的问题有点不清楚。如果您选择特定问题,如何计算问题?如果你想计算与特定问题相关的类别,你可以这样做(我假设你有外键而且在problem_categories表中没有重复项):

SELECT problem_id, COUNT(category_id) as num_categories
FROM problem_categories
WHERE problem_id = 266
GROUP BY problem_id

然后如果您需要打印更多问题信息,请加入问题表(为了提高效率,请始终获取详细信息)。如果你想要计算问题 - >类别 - >然后问题:

select p1.problem_id, (COUNT(DISTINCT(p2.problem_id)) - 1) as num_problems
FROM
(select problem_id, category_id FROM problem_categories where problem_id = 266) as p1
inner join 
(select problem_id, category_id FROM problem_categories) as p2
ON p1.category_id = p2.category_id
GROUP by p1.problem_id

如果您想显示问题详细信息,请再次加入问题表以获取它们。如果您希望num_problems和num_categories将第一个查询连接到第二个查询,然后再加入问题表以获取问题详细信息。即:

SELECT p1.problem_id, p1.num_categories, p2.num_problems 
FROM
(SELECT problem_id, COUNT(category_id) as num_categories
FROM problem_categories
WHERE problem_id = 266
GROUP BY problem_id) as p1
INNER JOIN
(SELECT p1.problem_id, (COUNT(DISTINCT(p2.problem_id)) - 1) as num_problems
FROM
(SELECT problem_id, category_id FROM problem_categories where problem_id = 266) as p1
INNER JOIN 
(SELECT problem_id, category_id FROM problem_categories) as p2
ON p1.category_id = p2.category_id
GROUP by p1.problem_id) as p2
ON p1.problem_id = p2.problem_id

答案 2 :(得分:1)

我认为以下内容应该有效:

SELECT category_name , categories.category_id, COUNT(*) as num_problems 
FROM problem_categories src JOIN CATEGORIES ON src.category_id = categories.category_id 
    JOIN problem_categories dest ON categories.category_id = dest.category_id 
WHERE problems.problem_id = 266
GROUP BY categories.category_id

您正在做的是问题ID,您希望查找类别ID,并从那里查找名称,并从类别ID中查看每个问题有多少(如果有)问题。 问题表的非使用是故意的。此外,要执行此操作,您需要以两个订单索引problem_categories。

更新:左连接可以是内连接,因为与问题266相关的每个类别至少有一个问题(问题266)。

答案 3 :(得分:1)

您可以准备类别和问题的数量。之后,您可以将其用作任何查询的一部分来提供信息。

SELECT categories.category_name ,
       categories.category_id,
       (select COUNT(*)
        from problem_categories
        where problem_categories.category_id =categories.category_id
       ) as num_problems
FROM categories 

可以在联接中使用(如果您希望存储在临时表中),以补充有关类别的信息:

select problem_categories.problem_id,
       temp_tab.category_name ,
       temp_tab.category_id,
       temp_tab.num_problems            
from problem_categories, 
(
SELECT categories.category_name ,
       categories.category_id,
       (select COUNT(*)
        from problem_categories
        where problem_categories.category_id =categories.category_id
       ) as num_problems
FROM categories 
) temp_tab
where problem_categories.problem_id =266
and problem_categories.category_id = temp_tab.category_id

如果你将它用于多个id或没有id条件,最好将类别中的信息存储在临时表中并加入它以便不重复计算。