将限制传递给" in"在mysql中查询

时间:2015-02-18 11:30:35

标签: mysql

我的表格如下: enter image description here

现在我想通过特定voices_id的限制。

我的意思是,如果我只需要3个特定voices_id的记录,那么该怎么做? (voices_id = 153& 154的3条记录)通过限制的地方?

2 个答案:

答案 0 :(得分:1)

这是你可以做的

select *
from table_name t1
where t1.voices_id in (153,154)
and
(
   select count(*) from table_name t2
   where t1.voices_id = t2.voices_id and t1.id <= t2.id
) <= 3
order by t1.voices_id
;

此处条件t1.id <= t2.id将为您提供每组最后3个条目,您可以在前3个条目中将其反转。

答案 1 :(得分:0)

就这么简单:

SELECT * FROM voices_talks WHERE voices_id=153 LIMIT 3
UNION ALL
SELECT * FROM voices_talks WHERE voices_id=154 LIMIT 3