Er.KT
Er.KT

Reputation: 2860

pass limit to "in" query in mysql

My table is like: enter image description here

now here I want to pass limit for particular voices_id.

I mean if I want just 3 records of particular voices_id then how to do so? (3 records for voices_id=153 & 154) where to pass limit ?

Upvotes: 2

Views: 202

Answers (2)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

This is what you can do

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
;

Here the condition t1.id <= t2.id will give you last 3 entry per group, you can reverse it for first 3 entry.

Upvotes: 1

Tomas M
Tomas M

Reputation: 7343

It is as simple as this:

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

Upvotes: 0

Related Questions