Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用GROUPBY子句从单个表检索数据_Mysql_Sql_Group By - Fatal编程技术网

Mysql 如何使用GROUPBY子句从单个表检索数据

Mysql 如何使用GROUPBY子句从单个表检索数据,mysql,sql,group-by,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'

我有一个表,其中我在单个列中接受图像、拒绝图像和更改图像,操作列中有操作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

如果不需要在单独的列中(这在您的描述中并不清楚),则可以执行以下操作:

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

否则,您将不得不透视消息列

您的上述查询是错误的。您可能从未获得消息列输出,因为在group by column中只有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;

这是一种方法。

试试这样的方法。
将sql表分成3列(图像id、操作、日期)

然后根据图像id更新action和date列,或插入到表中
ex sql
更新您的表格集操作class='Accepted Images',日期class='2013-01-01'其中image_id='1'
在表格中插入值('1','Accepted Images',2013-01-01)

然后您可以根据操作状态检索数据,
ex sql
从您的表中选择图像标识,日期,其中action='Accepted Images'group by date order by date asc limit 100


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

为接受和拒绝创建不同的查询。然后两者结合。

你能把你的问题说清楚一点吗?表的定义是什么?列的可能值是什么?结果应该是什么?听起来您需要一个联合查询。请尝试sql手册。id、用户id、位置id、标牌、操作、消息、日期时间18,1,1、KR54SWJ、2、“已接受的图像”、“2007-08-07 14:17:41”17,1,1、EJ03YXL、2、“已接受的图像”、“2007-08-07 14:17:30”16,1,1、K244MSB、2、“已接受的图像”、“2007-08-07 14:24”8,1,1、1、2、“已接受的图像”、“2007-08-07 14:06:25”抱歉无法正确编辑,我需要知道在特定的一天有多少接受的图像,被拒绝的图像
消息消息,仅日期,计数(消息)cou
“拒绝的图像”“接受的图像”,2007-08-07,79 82拒绝的图像”“接受的图像“,2007-08-08,52 56感谢克里希纳的快速回复,但我尝试了这一点,在我使用这一行where消息in('accepted Images','Rejected Images')where子句在此处不重要时,用两个不同的列获得了相同的结果accepted和rejecetd。计数块在用例中更为重要。若响应并没有改变,那个么在我的查询中尝试sum而不是count。如果你成功了,请告诉我Hi krishna,我同意你的说法,你的查询是正确的,我只得到接受图像的输出,但是你能告诉我如何在同一张表中得到被拒绝的图像吗?你没有在拒绝图像计数列中得到任何输出吗?请检查“被拒绝的图像”是否按照您的表数据正确拼写如果我更改了语句,我得到了被拒绝图像的结果,但接受的图像为空..其中('Rejected Images')中的消息我正在尝试获取被接受的图像和被拒绝的图像,我缺少一些内容..嗨,用户,感谢您的回复我的表格与您所说的一样,但我需要我的输出我在哪里可以在这一行的单个表格上获得接受的图像和拒绝的图像我只能检索一个操作“here action='accepted images'group by date”
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;