我在名为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......
请帮助我。
否
答案 0 :(得分:1)
在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;
答案 1 :(得分:0)
请检查以下查询
select distinct cata, id, title, autho, content from posts LIMIT 10
答案 2 :(得分:0)
您可以使用top 10
并串联如下选择的值
Select Top 10 'this is gaming posts ' + author + ' ' + cata + ' your content...' from
posts order by id ;