Essayez ceci ;)
Requête 1 :
select t1.*, t2.userCnt
from `posts` t1
inner join (
select max(`ID`) as `ID`, `user`, count(1) as userCnt
from `posts`
where `user` != '1'
and `post_type` = 'custom'
group by `user`
) t2 on t1.`ID` = t2.`ID` and t1.`user` = t2.`user`
order by t1.`ID` desc limit 4
Vérifiez ce SqlFiddle Résultats :
| ID | user | Date | title | status | post_type | userCnt |
|------|------|---------------------|-------|-----------|-----------|---------|
| 2783 | 5 | 2016-05-24 11:24:08 | Title | published | custom | 2 |
| 2759 | 3 | 2016-05-07 14:00:22 | Title | published | custom | 3 |
| 2757 | 12 | 2016-05-02 12:41:00 | Title | published | custom | 2 |
| 2683 | 15 | 2016-04-22 20:27:45 | Title | published | custom | 2 |
Sous-requête t2
obtiendra le maximum ID
dans chaque utilisateur lorsque user != '1' and post_type = 'custom'
, puis inner join
t1 avec t2 sur t1.ID = t2.ID and t1.user = t2.user
nous obtiendra un enregistrement qui a max ID
par chaque user
dans le tableau post
. Comme :"2783", "2759", "2757", "2683", "2681", "2652", "2630" , "2617", "2596", "2215".
Et enfin avec order by
et limit
, bien sûr, vous pouvez obtenir "2783", "2759", "2757", "2683". J'espère que je ne me suis pas trompé de question.