2012-09-17 67 views
1

我有一個表格,下面給出了記錄。我想要的是通過mediaid顯示第一條記錄最小第二條記錄平均值和第三條記錄最大值組的單選查詢條件?如何從mysql中的表中獲取min max和avg不同的行?

trans_id  playtime  mediaid(F.K.) 
--------  --------  ------------- 
1    32    2 
2    65    2 
3    45    3 
4    55    4 
5    60    3 
6    78    2 
7    45    4 
8    35    3 

結果應該

playtime  mediaid(F.K.) 
    --------  ------------- 
    32    2    (Min value) 
    58.33   2    (avg value) 
    78    2    (Max value) 
    35    3    (Min value) 
    46.66   3    (avg value) 
    60    3    (Max value) 
    45    4    (Min value) 
    50    4    (avg value) 
    55    4    (max value) 

,這是我的,但真正的查詢給出的結果,這是不是medtitle

select t.medTitle as 'Media', 
    min(TIMESTAMPDIFF(SECOND,s.dmtViewStart,s.dmtViewEnd))platime 
     from dbedetailing.tblmedia t 
     left outer join dbedetailing.tbldoctormediatracktest s on s.dmtMedId = t.medId 
     GROUP BY t.medTitle 
     union all 
     select t.medTitle as 'Media', 
    avg(TIMESTAMPDIFF(SECOND,s.dmtViewStart,s.dmtViewEnd))platime 
     from dbedetailing.tblmedia t 
     left outer join dbedetailing.tbldoctormediatracktest s on s.dmtMedId = t.medId 
     GROUP BY t.medTitle 
     union all 
     select t.medTitle as 'Media', 
    max(TIMESTAMPDIFF(SECOND,s.dmtViewStart,s.dmtViewEnd))platime 
     from dbedetailing.tblmedia t 
     left outer join dbedetailing.tbldoctormediatracktest s on s.dmtMedId = t.medId 
     GROUP BY t.medTitle ; 

安排,這是我真正的查詢給出的結果,但這種不按字幕排列

===數據庫連接

==表結構表tbldoctormediatracktest

|------ 
|Column|Type|Null|Default 
|------ 

    |//**dmtId**//|int(10)|No| 
    |dmtDocId|int(10)|No| 
    |dmtMedId|int(10)|No| 
    |dmtViewingDate|datetime|No| 
    |dmtViewStart|datetime|Yes|NULL 
    |dmtViewEnd|datetime|Yes|NULL 
    |dmaIsActive|enum('0', '1')|No| 
    |insDate|datetime|No| 
    |modDate|datetime|No| 
    == Dumping data for table tbldoctormediatracktest 
    Medid dmtViewStart   dmtViewEnd 
    |56|2012-08-24 11:41:14|2012-08-24 11:41:20 
    |52|2012-08-24 11:41:24|2012-08-24 11:42:12 
    |56|2012-08-24 11:41:14|2012-08-24 11:41:20 
    |52|2012-08-24 11:41:24|2012-08-24 11:42:00 
    |53|2012-08-25 12:32:27|2012-08-25 12:32:42 
    |55|2012-08-27 06:48:51|2012-08-27 06:49:19 
    |53|2012-08-27 06:49:25|2012-08-27 06:49:42 
    |53|2012-08-28 02:56:16|2012-08-28 02:56:47 
    |54|2012-08-28 02:57:58|2012-08-28 02:58:10 
    |52|2012-08-28 05:48:14|2012-08-28 05:49:03 
    |55|2012-08-28 07:16:00|2012-08-28 07:17:11 
    |52|2012-08-22 04:03:00|2012-08-22 04:03:55 
    |56|2012-08-28 07:16:00|2012-08-28 07:17:22 
    |56|2012-08-23 09:00:00|2012-08-23 09:02:00 
    |53|2012-08-16 04:00:00|2012-08-16 04:01:01 
    |54|2012-08-20 12:33:00|2012-08-20 12:34:14 
    |55|2012-08-16 04:00:20|2012-08-16 04:01:01 
    |52|2012-08-20 12:31:00|2012-08-20 12:31:14 
    |54|2012-08-20 12:33:14|2012-08-20 12:34:14 
    |55|2012-08-20 12:48:00|2012-08-20 12:49:14 
    |56|2012-08-20 12:33:00|2012-08-20 12:34:14 
    |53|2012-08-20 12:43:00|2012-08-20 12:44:14 
    |54|2012-08-20 12:33:00|2012-08-20 12:34:45 
    |55|2012-08-20 12:33:00|2012-08-20 12:34:34 
    |56|2012-08-20 12:33:00|2012-08-20 12:34:23 
    |52|2012-08-20 12:33:00|2012-08-20 12:34:18 
    |53|2012-08-20 12:33:00|2012-08-20 12:34:56 
    |54|2012-08-20 12:33:00|2012-08-20 12:33:56 
    |56|2012-08-20 12:33:00|2012-08-20 12:33:14 
    |56|2012-08-20 12:33:00|2012-08-20 12:34:14 



===Database dbedetailing 

== Table structure for table tblmedia 

|------ 
|Column|Type|Null|Default 
|------ 
|//**medId**//|int(10)|No| 
|medTitle|varchar(250)|No| 
|medDesc|varchar(250)|No| 
|medPath|varchar(250)|No| 
|medType|int(10)|No| 
|medSpeciality|int(10)|No| 
|medProduct|int(10)|No| 
|medIsActive|enum('0', '1')|No| 
|insDate|datetime|No| 
|modDate|datetime|No| 
== Dumping data for table tblmedia 

===數據庫dbedetailing

== Dumping data for table tblmedia 

|51|Tplanin 
|52|Casfung 
|53|Knee Surgery 
|54|Doriglen 
|55|Colymonas 
|56|inem 
|57|Casfungtest 

回答

7

如果你想在一個行的值,你可以這樣做:

select max(playtime), min(playtime), avg(playtime) 
from yourtable 
group by mediaid; 

如果您希望它們位於不同的行中,請使用UNION ALL

select max(playtime) playtime, mediaid, 'Max' Type 
from yourtable 
group by mediaid 
union all 
select min(playtime) playtime, mediaid, 'Min' 
from yourtable 
group by mediaid 
union all 
select avg(playtime) playtime, mediaid, 'Avg' 
from yourtable 
group by mediaid 
order by mediaid, playtime; 

看到SQL Fiddle with Demo

編輯#1,使用添加您的樣本數據:

select m.medTitle, 
    min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime 
from tbldoctormediatracktest s 
left join tblmedia m 
    on s.MedId = m.MedId 
group by m.medTitle 
union all 

select m.medTitle, 
    avg(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime 
from tbldoctormediatracktest s 
left join tblmedia m 
    on s.MedId = m.MedId 
group by m.medTitle 
union all 

select m.medTitle, 
    max(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime 
from tbldoctormediatracktest s 
left join tblmedia m 
    on s.MedId = m.MedId 
group by m.medTitle 
order by medTitle, playtime 

SQL Fiddle with Demo

+0

感謝名單。給予結果但不分組。我更新了我真正的查詢上半部分。 –

+0

@ManishMalviya如果你只運行一個查詢Max()'你會得到正確的結果嗎? – Taryn

+0

是的,它可以工作。由medTitle安排。但我想通過MedTilte –