如何根据类别从数据库中选择不同的数据

问题描述 投票:0回答:3

我在数据库中有一个名为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......

请帮我。

没有

php sql mysqli
3个回答
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;

0
投票

请您检查以下查询

select distinct cata, id, title, autho, content from posts LIMIT 10

0
投票

您可以使用top 10并连接下面选择的值

Select Top 10 'this is gaming posts ' + author + '   ' +  cata + ' your content...'  from 
 posts order by id ;
© www.soinside.com 2019 - 2024. All rights reserved.