Anusha Kottiyal
Anusha Kottiyal

Reputation: 3905

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..

Upvotes: 5

Views: 1105

Answers (1)

Didatus
Didatus

Reputation: 900

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

Upvotes: 1

Related Questions