Togrul Ceferli
Togrul Ceferli

Reputation: 121

Combining 2 SELECT expression with LIMIT in both

I'm developing an Android application. I have a database table and I want to request two or more different SELECT expression with different WHERE condition and the same LIMIT for each expression. My query is like:

(SELECT * FROM questions WHERE level=1 LIMIT 5)
UNION
(SELECT * FROM questions WHERE level=2 LIMIT 5)

When running application this query causes error that says:

near "UNION": syntax error: while compiling <here is my query>

When I omit the LIMIT it works well but LIMIT and ORDER BY don't work with UNION this way. My query is correct for MySQL according their documentation. But I couldn't find any SQLite documentation for my problem. So how should I query SQLite table for my needs?

Upvotes: 1

Views: 225

Answers (1)

a1ex07
a1ex07

Reputation: 37382

Try

 SELECT * FROM 
    (
      SELECT * FROM  
       (SELECT * FROM questions WHERE level=1 LIMIT 5)
       UNION  
       SELECT * FROM  
       (SELECT * FROM questions WHERE level=2 LIMIT 5)
    )

Upvotes: 3

Related Questions