我一直在进行小的查询。有一个名为Table_Activities的表。
Table_Activities (Act_id,Date,Activity_name,description)。
我们需要从这产生一份报告。用户将从下拉列表中选择要生成报表的、月份、和年份。
我们需要按活动名称显示该月份和年度组的所有活动。
示例-用户选择6月和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查询是什么??
发布于 2012-08-13 06:51:05
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发布于 2012-08-13 06:36:28
Select
DATE_FORMAT('date_column',"%D/%M/%Y") as `Date`,
other_column
from Table_Activities
where Month(Date) = Month('2012-06-01')
AND Year(Date) = Year('2012-06-01')https://stackoverflow.com/questions/11929131
复制相似问题