Let's say I have the following table with hundreds of toys of various colors...
---------------------------
ITEM | COST | COLOR
---------------------------
1 | 12.00 | BLUE
2 | 10.98 | RED
3 | 9.34 | BLUE
4 | 11.33 | WHITE
5 | 8.00 | YELLOW
. | . | .
. | . | .
. | . | .
If I wanted to select the three lowest priced blue toys, I could of course write...
SELECT * FROM TOYS WHERE Color = 'BLUE' ORDER BY COST LIMIT 3;
But, is there a way that I can select the a uniquely specified number of lowest priced yellow, white, and blue toys, as if I were saying...
SELECT * FROM TOYS WHERE COLOR = 'YELLOW' (LIMIT 1) AND COLOR = 'WHITE' (LIMIT 3) AND COLOR = 'BLUE' (LIMIT 2) ORDER BY COST
Is it possible to do this fully in MySQL without walking through the array in PHP?
In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.
No, you can't LIMIT subqueries arbitrarily (you can do it to a limited extent in newer MySQLs, but not for 5 results per group). This is a groupwise-maximum type query, which is not trivial to do in SQL.
Yes, you will notice a performance difference when dealing with the data. One record takes up less space than multiple records.
Yes, it is possible to use UPDATE query with LIMIT in MySQL.
Why not?
select * from toys where color = 'Yellow' (LIMIT 1)
UNION
select * from toys where color = 'White' (LIMIT 3)
UNION
select * from toys where color = 'Blue' (LIMIT 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With