我在数据库中有一个名为posts
的表,其中有五列名称
id, title , author , cata , content
* cata表示类别
我想获取10行不同类别的数据,如
$sql = "select * from `posts` by distinct cata";
就像是。
我想要这个结果
1.this is gaming posts author(Mauk) cata(Gaming) content......
2.this is glossary posts author(Mauk) cata(glossary) content......
3.this is shoes posts author(Mauk) cata(shoes) content......
4.this is garments posts author(Mauk) cata(garments) content......
5.this is you tube posts author(Mauk) cata(YouTube ) content......
6.this is blogging posts author(Mauk) cata(blogging ) content......
7.this is editing posts author(Mauk) cata(editing) content......
8.this is electronics posts author(Mauk) cata(electronics) content......
9.this is programming posts author(Mauk) cata(programming ) content......
10.this is news posts author(Mauk) cata(news) content......
请帮我。
没有
在MySQL 8+中,您可以:
select p.*
from posts p
order by row_number() over (partition by cata order by id)
limit 10;
在早期版本中,您可以通过执行以下操作获取每个类别的最新帖子:
select p.*
from posts p
where p.id = (select max(p2.id)
from posts p2
where p2.cata = p.cata
)
limit 10;
请您检查以下查询
select distinct cata, id, title, autho, content from posts LIMIT 10
您可以使用top 10
并连接下面选择的值
Select Top 10 'this is gaming posts ' + author + ' ' + cata + ' your content...' from
posts order by id ;