Probeer dit;)
Query 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
Controleer deze SqlFiddle Resultaten :
| 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 |
Subquery t2
krijgt de max ID
in elke gebruiker wanneer user != '1' and post_type = 'custom'
, dan inner join
t1 met t2 op t1.ID = t2.ID and t1.user = t2.user
zal ons een record bezorgen met max ID
door elke user
in tabel post
. Zoals:"2783", "2759", "2757", "2683", "2681", "2652", "2630", "2617", "2596", "2215".
En als laatste met order by
en limit
, natuurlijk kunt u "2783", "2759", "2757", "2683" krijgen. Ik hoop dat ik je vraag niet verkeerd heb begrepen.