Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WHERE with LIMIT MySQL

Tags:

php

mysql

Using PHP, is it possible to select multiple rows from one table, in a MySQL database, with different WHERE clauses having a specific LIMIT for each WHERE clause.

For example:

SELECT * FROM the_table WHERE color = 'blue' LIMIT 5 OR color = 'red' LIMIT 10

I know the above statement does not work. But is there a way to do this with a single call to the database?

like image 273
Marcus Avatar asked Dec 16 '22 23:12

Marcus


2 Answers

You can use a UNION, if I understand your post correctly:

(SELECT * FROM the_table WHERE color = 'blue' LIMIT 5)
UNION ALL
(SELECT * FROM the_table WHERE color = 'red' LIMIT 10)
like image 66
Josh Davis Avatar answered Jan 02 '23 15:01

Josh Davis


SELECT * FROM the_table WHERE color = 'blue' LIMIT 5

UNION

SELECT * FROM the_table WHERE color = 'red' LIMIT 10 ;
like image 38
Romain Deveaud Avatar answered Jan 02 '23 16:01

Romain Deveaud