Coba ini;)
Kueri 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
Periksa SqlFiddle ini Hasil :
| 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 |
Subkueri t2
akan mendapatkan ID
maksimum di setiap pengguna ketika user != '1' and post_type = 'custom'
, lalu inner join
t1 dengan t2 pada t1.ID = t2.ID and t1.user = t2.user
akan memberi kami catatan yang memiliki ID
maks oleh setiap user
dalam tabel post
. Seperti :"2783", "2759", "2757", "2683", "2681", "2652", "2630", "2617", "2596", "2215".
Dan terakhir dengan order by
dan limit
, tentu saja Anda bisa mendapatkan "2783", "2759", "2757", "2683". Semoga saya tidak salah mengartikan pertanyaan Anda.