0

私は小さなクエリで立ち往生しています。Table_Activities という名前のテーブルがあります。

Table_Activities ( Act_id, Date, Activity_name, description)。

これからレポートを生成する必要があります。ユーザーは、レポートを生成する年をドロップダウン リストから選択します。

その月と年のグループのすべてのアクティビティをアクティビティ名で表示する必要があります。

例 - ユーザーはJune2012を選択します。

レポートは-

園芸

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 クエリは何になるのでしょうか??

4

3 に答える 3

1

Activity_nameで特定の月と年のグループのデータを取得するには、次のことを試してください。

SELECT Activity_name
,GROUP_CONCAT(DATE_FORMAT(Date,"%d/%m/%Y")) as `Date`
,GROUP_CONCAT(Description) as `Description`
FROM Table_Activities 
WHERE MONTH(Date) = MONTH('2012-06-01')
AND YEAR(Date) = YEAR('2012-06-01')
GROUP BY Activity_name 

出力

ACTIVITY_NAME日付の説明
-------------------------------------------------- ---------------------------------
ガーデニング2012年1月6日、2012年6月27日彼らは100本の木を植えました。、説明3
トレーニング2012年12月6日、2012年6月28日説明2、説明4

このSQLFiddleを参照してください

于 2012-08-13T06:34:34.933 に答える
1
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
;

SQL FIDDLE HERE.

以下のように出力します。

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-13T06:51:05.203 に答える
0
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')
于 2012-08-13T06:36:28.447 に答える