I'm having trouble reducing 12 mysql queries into one call. I need to group the results by product_type_id in a random order and then limit each group by 7:
SELECT * FROM products WHERE product_type_id=1 AND deleted='n' ORDER BY RAND() LIMIT 7;
SELECT * FROM products WHERE product_type_id=2 AND deleted='n' ORDER BY RAND() LIMIT 7;
...
SELECT * FROM products WHERE product_type_id=12 AND deleted='n' ORDER BY RAND() LIMIT 7;
There are 12 total queries currently for each product_type_id (1,2,3,...12). Any help appreciated.
So you need 7 random records for each product type (7 * 12 records). At first sight you're just looking for a union.
(SELECT * FROM products WHERE product_type_id=1 AND deleted='n' ORDER BY RAND() LIMIT 7)
UNION ALL
(SELECT * FROM products WHERE product_type_id=2 AND deleted='n' ORDER BY RAND() LIMIT 7)
UNION ALL
...
UNION ALL
(SELECT * FROM products WHERE product_type_id=12 AND deleted='n' ORDER BY RAND() LIMIT 7)
If you need to order the whole resultset then you can use an ORDER BY clause after the whole UNION.
Related
Here is my table test with values:
Price
----------
300
600
900
1000
1800
2000
I want to query such that when I search for 300 ,I should get 4 records 300,600,900,1000.
If I search for 900, I can get 600,900,1000,1800.
i.e. Two records <=900 and Two record >900
Here is the query I tried :
SELECT * FROM table h where CONDITIONS
and (price in (select price from table where price <=900) // I want to add LIMIT 2 in this subquery
or price in (select price from table where price >900)//LIMIT 2
)
order by FIELD(price ,900) DESC limit 5;
I searched a lot on stack overflow,but nothing worked. Please help .
You can try the following...
select * from ((select h.* from table_name h where amount <=300 order by amount desc limit 2)
union
(select h.* from table_name h where amount >300 order by amount limit 2))
derived_table order by FIELD(amount,300) desc;
MySQL doesn't support LIMIT in WHERE IN/EXSISTS/ANY/SOME subqueries, you can do this with UNION
(SELECT * /* this should be a columnlist */
FROM tablename
WHERE price < 900
ORDER BY price LIMIT 2)
UNION
(SELECT * /* this should be a columnlist */
FROM tablename
WHERE price >= 900
ORDER BY price LIMIT 2)
The parenthesis around each select are crucial.
See SQL Fiddle
Using union because this mysql version don't accept limit in subqueries
select * from table where price <=900 limit 2 union select * from table where price > 900 limit 2
I want to select 5 random rows from a table but only from the 20 most recent rows. I know the 2 statements separately would be something like:
SELECT * FROM table ORDER BY RAND() LIMIT 5
SELECT * FROM table ORDER BY date DESC LIMIT 20
How would I combine these 2 statements so it will select 5 random rows from the 20 most recent rows? Thanks.
Use an nested select
SELECT foo.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20 ) as foo
ORDER BY RAND() LIMIT 5
Simply nest them:
SELECT * FROM (
SELECT * FROM table ORDER BY date DESC LIMIT 20
) ORDER BY RAND() LIMIT 5
Look up subqueries!
SELECT d.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20) as d ORDER BY RAND() LIMIT 5;
I need to take 10 rows from table. It has to be random but in that table I have only 5 rows.
And so if I select:
SELECT * FROM `names` order by rand() limit 10
But this query returns me only 5.
How to get five more with repeat?
SELECT m.* FROM names m,names n order by rand() limit 10
This may be what you wanted
Try like
SELECT * FROM names
UNION ALL
SELECT * FROM names
ORDER BY RAND() LIMIT 10;
Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1
Can someone help me with this query.
I have this table for sample:
FILE Table
UID
file
uploaded_on_date
view_count
What I want to consider is the top 30 most recent uploaded file and top 30 most viewed file then randomly select from them and limit by 10.
I am new to this mysql complex queries. A sample query would be good and i will be able to understand it.
Thanks.
SELECT
*
FROM
(
SELECT
*
FROM
tablefile
ORDER BY
uploaded_on_date DESC
LIMIT 30
UNION SELECT
*
FROM
tablefile
ORDER BY
view_count DESC
LIMIT 30
)
ORDER BY
RAND()
LIMIT 10;
select * from (
select * from table order by upload_on_date desc limit 30
union
select * from table order by view_count desc limit 30) t
order by rand() limit 10