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 Fiddle 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 に答える