twistedpixel
twistedpixel

Reputation: 1225

In SQL, can you specify a limit on a row condition?

For example, a table contains messages with a is_read field. Is there a way to use LIMIT to say "Give me all rows in this table, but I want to LIMIT the number of rows returned where is_read is TRUE?"

EDIT

Sorry, I may have not explained this well. I want all the rows where is_read is FALSE but be able to limit the rows returned where is_read is TRUE. I hope that makes more sense?

Upvotes: 1

Views: 1079

Answers (2)

Bort
Bort

Reputation: 7618

You could union two queries together, one for each condition:

SELECT * FROM theTable WHERE is_read = 'false'

UNION ALL

SELECT * FROM theTable WHERE is_read = 'true'
LIMIT 42

Upvotes: 4

user1864610
user1864610

Reputation:

Use a UNION on two queries like this:

SELECT * FROM TableName WHERE is_read = 'false'
UNION
SELECT * FROM TableName WHERE is_read='true' limit 5

Upvotes: 1

Related Questions