我陷入了小查询。有一个名为 Table_Activities 的表。
Table_Activities(Act_id
、Dat
e、Activity_name
、说明
)。
我们需要据此生成一份报告。用户将从下拉列表中选择他想要生成报告的月和年。
我们需要按事件名称显示该月和该年组的所有事件。
示例 - 用户选择六月和2012。
报告将是-
园艺
01/06/2012 - They have planted 100 trees.
14/06/2012 - something
27/06/2012 - something
培训
02/06/2012 - Detail description
15/06/2012 - something
28/06/2012 - something
我的问题是用什么mysql查询来获取这种格式的数据?
最佳答案
select `Date`,description from tm_activities
where month(`Date`)='6' and year(`Date`)='2012'
order by Activity_name,`date`
要返回问题中所示的确切格式,请尝试以下操作:
select concat(if(actdate='',activity_name,date_format(actdate,'%d/%m/%y')),if(description<>'',concat(' - ',description),'')) as labelm from
(
(select ActDate,description,activity_name from tm_activities where month(ActDate)='6' and year(ActDate)='2012'
)
union all
(Select distinct '','',activity_name from tm_activities where month(ActDate)='6' and year(ActDate)='2012')
)m order by activity_name,actdate
;
输出如下:
Gardening
01/06/12 - They have planted 100 trees.
27/06/12 - Gar 2
Training
12/06/12 - Training 1
28/06/12 - Traning 2
30/06/12 - Traning 3
关于mysql - 对此可以优化的查询是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11929131/