Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two SQLite SELECT statement with two conditions

Here i want to select questions with two different difficulty from same table. I am using query :

readAllQuestions = [NSString stringWithFormat: @"SELECT * FROM tbl_questions WHERE difficulty IN(1,3) AND approved = 1"];

Its working. Now i want to limit the questions to 100 and it includes 50 questions with difficulty 1 and other 50 with difficulty 3. Using LIMIT only give first 100 questions.

How to do this without using two different queries?? Please help..

like image 725
Anusha Kottiyal Avatar asked Nov 13 '22 14:11

Anusha Kottiyal


1 Answers

You can do so with subselects: (assuming the primary key is called 'id')

SELECT * FROM tbl_questions WHERE (id IN (SELECT id FROM tbl_questions WHERE difficulty = 1 LIMIT 0,50) OR id IN (SELECT id FROM tbl_questions WHERE difficulty = 3 LIMIT 0,50)) AND approved = 1
like image 52
Didatus Avatar answered Nov 15 '22 05:11

Didatus