How to select a specific record from MySQL database How to select a specific record from MySQL database codeigniter codeigniter

How to select a specific record from MySQL database


You can use sum() and select case, std_id above 3 will be 0

select * from (    select sum(case when std_id in (1, 2, 3) then 1 else 0 end) tot        , course_id    from std_course    group by course_id) t1 where t1.tot <= 3


You can try this using join:

select * from std_course left outer join course on course.id= std_course.course_id where std_id <=3


The query that work for me after the changings in query Answered by @ϻᴇᴛᴀʟ is:

select * from ( select sum(case when std_id in (1,2,3) then 1 else -1 end) tot , course_id from std_course group by course_id) t1 where t1.tot = 3