MySQL Query add limit in SubQuery - mysql

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

Related

mysql select highest and lowest values with limit in one statement

I need to pull the 5 highest prices and 5 lowest prices from a table products on column prices. I thought I could do two select in one stmt like below, but I think you cannot because it is the same table? I have done similar stmts and it worked but with different tables.
SELECT products.* AS fullcount, (SELECT * FROM products ORDER BY price ASC LIMIT 5) AS highest, (SELECT * FROM products ORDER BY price DESC LIMIT 5) AS lowest FROM products
What am I doing wrong or should I be using a different approach?
Use UNION to combine the results of queries that get the highest and lowest rows.
SELECT *
FROM (
SELECT *
FROM products
ORDER BY price DESC
LIMIT 5) x
UNION (
SELECT *
FROM products
ORDER BY price ASC
LIMIT 5
) y

How do I select 5 random rows from the 20 most recent rows?

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;

Select random rows in mysql and limit within each group

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.

How do I select the 1st row from a selection of 10 random rows

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

MySQL: Select records where COUNT

I'm trying to select 1000 customers that have placed exactly 1 order. Everything is in the Orders table.
select * from Order having count(CustomerID) = 1 limit 1000
So basically all records that have only one occurence of the CustomerID in the entire table. This returns an empty result set and there are 100,000s in the table.
You need to GROUP in order to (meaningfully) use the HAVING clause. The following works as expected:
SELECT
*
FROM
`Order`
GROUP BY
CustomerID
HAVING
COUNT(CustomerID) = 1
LIMIT 1000
UPDATE
Adding WHERE (see comments):
SELECT
*
FROM
`Order`
WHERE
Language = 'EN'
GROUP BY
CustomerID
HAVING
COUNT(CustomerID) = 1
LIMIT 1000
SELECT * FROM `Order`
GROUP BY CustomerID
HAVING COUNT(CustomerID) = 1
LIMIT 1000
try
select count(CustomerID) as counter ,o.* from Order o
group by CustomerID having counter = 1 limit 1000
Add group by to query for correct output, e.g:
select *
from Order
group by CustomerID
having count(CustomerID) = 1
limit 1000
SELECT CustomerID FROM Order GROUP BY CustomerID HAVING COUNT(*) = 1 LIMIT 1000