如何使用子句获取限制记录

时间:2015-01-20 11:29:29

标签: php mysql

我有以下查询 我正在将id作为字符串

SELECT * from table_name 
where id in (1,2,3,4) 
order by created desc 
limit 0,4

此查询只给我4条记录

我想从每个ID中获取4条记录

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

select * from 
(
    SELECT * from table_name where id = 1 order by created desc limit 4
    union all
    SELECT * from table_name where id = 2 order by created desc limit 4
    union all
    SELECT * from table_name where id = 3 order by created desc limit 4
    union all
    SELECT * from table_name where id = 4 order by created desc limit 4
) tmp
order by created desc