在单个表

时间:2016-03-05 06:36:15

标签: php mysql

我已经创建了一个表格,其中输入了3个状态,Solved,Unsolved和OnHold。我已经创建了以下查询,我想创建条形图,它应该显示3条状态日期明智。

select a.date, a.Solved,b.Unsolved, c.OnHold 
from (select count(prob_stat) as Solved, date from delivery 
where prob_stat='Solved' group by date) a 
inner join (select count(prob_stat) as Unsolved, date from delivery 
where prob_stat='Unsolved' group by date) b on a.date=b.date 
inner join (select count(prob_stat) as OnHold, date from delivery 
where prob_stat='OnHold' group by date) c on a.date=c.date

但它不起作用,我只能看到1条记录。有人可以帮忙吗?

普拉

2 个答案:

答案 0 :(得分:0)

试试这个。

使用CASE表达式。

<强>查询

select `date`,
sum(case prob_stat when 'Solved' then 1 end) as Solved,
sum(case prob_stat when 'Unsolved' then 1 end) as Unsolved,
sum(case prob_stat when 'OnHold' then 1 end) as OnHold
from delivery
group by `date`;

答案 1 :(得分:0)

你可以尝试...... 工作正常

SELECT 
  date,
  COUNT(CASE prob_stat WHEN 'Solved' THEN 1 END ) AS Solved, 
  COUNT(CASE prob_stat WHEN 'Unsolved' THEN 1 END) as Unsolved,
  COUNT(CASE prob_stat WHEN 'OnH' THEN 1 END) AS OnHold 
FROM delivery 
GROUP BY date;