sql关于相同父ID最多取3记录
Jun 23, 2016 pm 02:08 PM 表 imginfo
id fid miaoshu
1 3 aaaaaaa
2 3 sssssss
3 3 fffff
4 4 uuuuuuuu
5 5 gfgfgfgfgf
6 5 ddfdfdfd
7 9 fdffdfd
8 9 dfdfdfd
9 9 dfdfdf
10 9 popopop
11 3 tgtrtyrtyry
12 5 fdsjfdfdl
SELECT * FROM `imginfo` WHERE fid in(3,5,9) ORDER BY `id` DESC
上面这条语句fid 3,5,9全部内容都获取了。
现在我只想每条相同fid最多获取3条,排序不变,sql语句怎么写?
请大家指教请高人指教。不胜感激!
回复讨论(解决方案)
你SQL取出来在php那端每个值取3个不就结了?
拍完序之后仅获取前三条的话可以用limit0,3;
select a.* from imginfo a where 3 > (select count(*) from imginfo where fid = a.fid and id > a.id and fid in(3,5,9)) and fid in(3,5,9) order by a.fid,a.id
SQL语句如下:
select a.* from imginfo a where 3 > (select count(*) from imginfo where fid = a.fid and id > a.id and fid in(3,5,9)) and fid in(3,5,9) order by a.fid,a.id
如果只是按ID降序,排序改下就好
select a.* from imginfo a where 3 > (select count(*) from imginfo where fid = a.fid and id > a.id and fid in(3,5,9)) and fid in(3,5,9) order by a.id desc
可以这样的哦,学习了。
学习了!谢谢版主。
select a.* from imginfo a where 3 > (select count(*) from imginfo where fid = a.fid and id > a.id and fid in(3,5,9)) and fid in(3,5,9) order by a.fid,a.id
版主 想问下 我想按照条数的多少来排序该怎么做呢?比如fid 为3的有5条 为9的有10条 为5的有3条 排序就为 9 3 5 请问怎么实现了 谢谢了

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium)

Travailler avec les données de session Flash dans Laravel

Construisez une application React avec un Laravel Back End: Partie 2, React

Misque de réponse HTTP simplifié dans les tests Laravel

Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST

12 meilleurs scripts de chat PHP sur Codecanyon
