SQL order top rows differently than the rest - mysql

Is there any way how to order top rows in a query in a different way than the rest? For example, if I have a list of products with name and price, I would like to get the list ordered in a way that 10 most expensive products are on top ordered by price desc, and the rest below is ordered by product name.
What comes in my mind is something like this:
SELECT id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
ORDER BY name
but this query does not execute, it prints Incorrect usage of UNION and ORDER BY, furthermore if I wrap the selects into another selects, it prints LIMIT & IN/ALL/ANY/SOME subquery. Any idea?

select * from
(
SELECT 1 a, id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT 0, id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
)
order by a*price desc, name

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 can I get a DISTINCT from mySQL but only for 1 field?

I have a row with products and I'd like to get 10 random records, but a maximum of 1 row per user_id. Right now I have this:
SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
ORDER BY RAND() DESC LIMIT 0,12
I tried doing a SELECT DISTINCT user_id, ... but that doesn't work. The table has 100's of products and each user_id may have multiple ones, but I'd like to retrieve a maximum of 1 per user_id, but still a total of 10.
Is that possible at all without a more complex structure?
I may be missing something, but have you tried doing a GROUP BY?
SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
GROUP BY user_id -- SPECIFY FIELD HERE
ORDER BY RAND() DESC
LIMIT 0,12
This will group one row per user, or whichever field you desire to group by.
Try something like that with grouping in main query after random ordering in subquery:
SELECT * FROM
(SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
ORDER BY RAND()) AS subquery
GROUP BY user_id
LIMIT 0,10

SQL statement Max(Count(*))

Basically I have a review table for product. The attributes are reviewID, reviewCustName, reviewText, productID. So I wonder is there any ways to count the product with most reviews? Here is my SQL statement:
SELECT productID, count(*) AS mostReviews, MAX(mostReviews) FROM sm_review GROUP BY productID;
I wonder is it possible to write such SQL statement? Or i there any better way?
Thanks in advance.
You can use the following to get the result. This gets the total count for each product but when you order the count in a descending order and apply LIMIT 1 it returns only the product with the most reviews:
select count(*) total
from sm_review
group by productId
order by total desc
limit 1
It should just be;
SELECT count(*) AS num_reviews FROM sm_review
GROUP BY productID ORDER BY num_reviews DESC LIMIT 1;
Note the ORDER BY num_reviews and the LIMIT 1 which limits the number of results.

MySQL select random rows with order by

I want to retrieve random rows from table but this rows must be order in category.
select category,
(select order_number
from orders
where order_number in (123,125,128,129,256,263,966,258,264,159,786)
order by rand())
from orders
order by category
This is the query I tried. But that retrieves whole data in table.
Worked query ;
SELECT category,order_number FROM (
SELECT category,order_number
from orders
where order_number in (`$order_numbers_variable`)
order by rand()
) order by category
I assume the requirement is:
Retrieve 'N' random rows from a table sorted by 'category'.
Lets assume N is 10. If you want to change the number of rows, then change it in the LIMIT clause.
SELECT * FROM (
SELECT category from orders ORDER BY rand() ASC LIMIT 10
) AS innerResult
ORDER BY innerResult.category

MySQL query to find top items

I have a table called order_list with the following fields
order_id
user_id
item_id
count
date
order_status
every time users place order, this table used to save order details. so how can I write my SQL query to find top 10 items based on the sum of ordered count?
SELECT item_id, SUM(count) FROM order_list GROUP BY item_id ORDER BY SUM(count) DESC LIMIT 0,10
SELECT *
FROM table_name
GROUP BY item_id
ORDER BY count DESC
LIMIT 10
select item_id, sum(count) as total
from order_list
group by item_id
order by total desc
limit 10