0

我想要的是将静态字符串连接到数据集,我想将字符串作为 min、avg、max 添加到获取的数据中。当我尝试给出一些错误时?

我的查询

select m.medTitle, 
min(TIMESTAMPDIFF(SECOND, s.dmtViewStart, s.dmtViewEnd)) playtime
from tbldoctormediatracktest s
left join tblmedia m on s.dmtMedId = 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.dmtMedId = 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.dmtMedId = m.medId
group by m.medTitle
order by medTitle,  playtime;

我正在尝试什么。

select concat(m.medTitle,'minimum'), 
  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 concat(m.medTitle,'average'), 
  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 concat(m.medTitle,'maximum'),  
  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
4

2 回答 2

2

尝试这个:

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

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

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


SQL 小提琴 domo 1

SQL 小提琴演示 2

SQL 小提琴演示 3

于 2012-09-18T05:55:18.177 回答
0
select concat(m.medTitle, 'min') 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 concat(m.medTitle, 'avg') 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' 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

从链接更新了您的查询。我注意到您需要在连接后为 m.medTitle 提供别名。

任何一个concat(m.medTitle, 'min')m.medTitle || 'min'都可以用于连接。

于 2012-09-18T05:13:40.950 回答