如何使用group by子句从单个表中检索数据

时间:2013-07-29 11:59:03

标签: mysql sql group-by

我有单人桌,我已经接受图像,被拒绝的图像,并在单列中更改了图像,动作列具有动作2接受的图像,动作3 - 注入图像,动作4-更改图像。我能够运行“接受的图像”,但我也想要一个不同的列来拒绝图像。但不能得到那个。我是mysql的新手,请帮帮我。如何为被拒绝的图像获取不同的列

select  message,  date(datetime) as dateonly ,count(message)
from customer_1.audit_trail where message in ('Accepted Images') 
group by dateonly  order by dateonly asc limit 100;


 message,      dateonly, count(message)
"Accepted Images",2007-08-07,  79
"Accepted Images",2007-08-08,52

5 个答案:

答案 0 :(得分:0)

如果它不需要在单独的列中(从您的描述中不是很清楚),那么您可以这样做:

select  message,  date(datetime) as dateonly, count(message)
from customer_1.audit_trail 
group by message, dateonly  
order by dateonly asc limit 100;

否则你必须转动消息列

答案 1 :(得分:0)

您的上述查询错误。您可能永远不会获得消息列输出,因为在您的分组中,您只有dateonly。

select  date(`datetime`) as dateonly ,
count(case when message= 'Accepted Images' then 1 else 0 end ) as accepted_image_count,
count(case when message= 'Rejected Images' then 1 else 0 end ) as  rejected_image_count
from customer_1.audit_trail where message in ('Accepted Images','Rejected Images') 
group by dateonly  order by dateonly asc limit 100;

答案 2 :(得分:0)

SELECT  message,  date(datetime) as dateonly ,
sum(CASE message when 'Accepted Images' then 1 else 0 end) as CNT_Accepted,
sum(CASE message when 'Rejected Images' then 1 else 0 end) as CNT_Rejected
FROM customer_1.audit_trail 
WHERE message in ('Accepted Images') 
GROUP BY dateonly, message
ORDER BY dateonly asc limit 100;

这是一种方式。

答案 3 :(得分:0)

试试这样的事情。
将你的sql表分成3列(image_id,action,date)

然后根据图像ID更新动作和日期列或插入表格;
ex sql
更新yourTable set action ='接受图片',日期='2013-01-01',其中image_id ='1';
插入yourTable值('1','接受的图像',2013-01-01);

然后您可以根据操作状态检索数据,
ex sql
从你的表中选择image_id,date,其中action ='Accepted Images'组按日期顺序排列asc limit 100;

如果需要任何其他列,您可以根据自己的要求进行管理。请在下次尝试清楚说明您的问题。

答案 4 :(得分:0)

为已接受和已拒绝创建不同的查询。然后是UNION。