Mysql
 sql >> Base de données >  >> RDS >> Mysql

Wordpress mysql groupe par | commandé par

vos identifiants sont incrémentiels. utilisez-le.

select ...
from 
.....
where id post_in 
(select max(post_id) from table group by category)

si vous savez combien de catégories il y a, vous pouvez le faire encore mieux en utilisant

where post id in 
(select post_id from table where category=1 order by time desc limit 1
union
select post_id from table where category=2 order by time desc limit 1
union
select post_id from table where category=3 order by time desc limit 1
union
select post_id from table where category=4 order by time desc limit 1)

ou vous pouvez utiliser des paramètres qui vous donneront un résultat parfait mais une requête très lente

select * from
(select 
@rn:=if(@prv=category_id, @rn+1, 1) as rId,
@prv:=category_id as category_id,
timestamp,
other columns
from (select category_id, timestamp, other columns from ... )a
join
(select @prv:=0, @rn:=0)tmp
order by 
category_id , timestamp desc) a
where rid<=1