我有一张桌子,就像这样:
表新闻
Id Name Other_Columns
-------------------------
1 A data 1
2 A data 2
3 A data 3
4 B data 4
5 B data 5
6 C data 6
我需要检索这样的东西
Group A
----------
data 1
data 2
data 3
Group B
----------
data 4
data 5
Group C
----------
data 6
有人可以帮我解决MySql声明吗?
答案 0 :(得分:0)
你真正想要的并不是很清楚,但是这样做了吗?
select "Group A"
union all
select Other_Columns from news where name = "A"
union all
select "Group B"
union all
select Other_Columns from news where name = "B"
union all
select "Group C"
union all
select Other_Columns from news where name = "C"
答案 1 :(得分:0)
这很简单。查询将如下:
对于A组:
SELECT Other_Columns FROM news WHERE Name = 'A';
地点' B'和' C'取代' A',以获得其他群组。
答案 2 :(得分:0)
应该这样做:
select asdf.Name, asdf.Other_Columns from (
select
if(@prev != q.Name, @rownum:=1, @rownum:=@rownum+1) as rownumber, @prev:=q.Name, q.*
from (
select
Name,
Other_Columns
from
yourTable yt
, (select @rownum:=0, @prev:='') r
order by Id, Name, Other_Columns
)q
)asdf
where asdf.rownumber <= 5
这将为您提供最新的5个。格式化数据是在表示层完成的,如上所述。