Query double sort - mysql

I have a table of items and which supplier they came from: tabel items
Ttem ID - Item Title - Supplier - Date added
A supplier may supply multiple items, neither the title nor the supplier are unique.
I want to show the latest added 56 items, and, from those 56 items I want to show the top ten suppliers
So individually I have:
select *
from items
order by dateadded desc
limit 56
And for the suppliers
select count(supplier) as howmany
from items
group by supplier
order by howmany desc
limit 10
My question is how to show the top ten suppliers of the 56 items selected
Thanks for any help

Just combine your two statements and use the first one as a subselect:
select count(*) as howmany from (
select * from items order by dateadded desc limit 56
) top_items group by supplier order by howmany desc limit 10
Update: You originally had count(supplier), which should work, but is not as clear as it could be (it sort of looks like you are trying to count the number of suppliers). I changed to count(*) to highlight the fact that you are counting the rows (number of items) for each supplier.

This will get you the top 10 suppliers of the most recent 56 items:
SELECT COUNT(supplier) AS howmany
FROM items
WHERE item_id IN (
SELECT t2.item_id
FROM items t2
ORDER BY t2.dateadded DESC
LIMIT 56
)
GROUP BY supplier
ORDER BY howmany DESC
LIMIT 10;

Related

How can I select the row from a table whose 1 field is maximum or minimum?

In my table I have a field name rt_stars which includes integer anyone between 1 to 5. Suppose there are 5 rows having rt_stars 4,4,3,2,1 respectively. Here, 4 is the highest and 1 is the lowest. If I want to select the row with the maximum value 4 how can I do that? Since there are two 4 here the last one will be selected. Can I have query something like this?
SELECT * FROM ratings WHERE MAX(rt_stars) ORDER BY rt_id DESC LIMIT 1
I know this is wrong but that's how I want to select all the values from the rows if the rt_stars field has the maximum value in it. How can I achieve this kind of a query?
You can select one row using:
select r.*
from ratings r
order by rt_stars desc, rt_id desc
limit 1;
The problem with your query is that you cannot use max() in the where clause. Beyond that, you don't need aggregation at all -- just ordering the rows and then selecting the first one.
If you want all rows with the maximum stars you can do:
select *
from ratings
where rt_stars = (select max(rt_stars) from ratings)
If you just want one of them randomly you can do:
select *
from ratings
order by rt_stars desc
limit 1
I think the below SQL code will help.
SELECT * FROM ratings WHERE rt_stars = MAX(rt_stars) ORDER BY rt_id DESC;
Sorry the code modified below
SELECT * FROM ratings WHERE rt_stars = ( SELECT MAX( user_05_pk ) FROM ratings )
ORDER BY rt_id DESC;

select rows mysql order by custom value From the bottom of the table to the top of the table

I want Last eight posts if productgroup='برنامه نویسی' And the results that they have subject="css" Have a higher priority
this is my code :
$query = "SELECT productcode FROM (SELECT * FROM product ORDER BY productcode DESC) secondTab
WHERE productgroup='برنامه نویسی' ORDER BY FIELD(subject,'css') DESC limit 8";
But get result From the top of the table to the bottom of the table:
6
7
8
10
14
20
34
2
The ORDER BY clause inside the subquery is useless.
Sort the results like this:
SELECT productcode
FROM product
WHERE productgroup = 'برنامه نویسی'
ORDER BY FIELD(subject,'css') DESC, productcode DESC
LIMIT 8
If you want product code 21 to be last, use two order by keys:
order by (productcode = 21) asc, -- put it last
productcode desc
MySQL treats boolean values as integers in a numeric context, with 0 for false and 1 for true. Hence, "false" is ordered (using ASC) before true values.

SQL order top rows differently than the rest

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

Advanced mysql query which sort down specific records on result set irrespective of its default sorting?

I have a query which actually have a sorting using order by clause. i have a table like following...
user_id user_name user_age user_state user_points
1 Rakul 30 CA 56
2 Naydee 29 NY 144
3 Jeet 40 NJ 43
.....
i have following query...
select * from users where user_state = 'NY' order by user_points desc limit 50;
This gives me the list of 50 people with most points. I wanted to give least preference to few people who's id's were known. Incase if i do not have enough 50 records then those id's should come in the last in the list. I do not want the users 2 and 3 to come on top of the list even though they have higher points... those people should come on the last of the list from the query. Is there any way to push specific records to last on result set irrespective of query sorting ?
If you want to move specific records (like user_id = 2 and 3) down to the list; Then you can run below Query:
mysql> select *,IF(user_id=2 or user_id=3,0,1) as list_order from users where user_state = 'NY' order by list_order desc, user_points desc limit 50;
select * from (
select *
from users
where user_state = 'NY'
-- this order by ensures that 2 and 3 are included
order by case when user_id in (2,3) then 1 else 2 end, user_points desc
limit 50
) as top48plus2n3
-- this order by ensures that 2 and 3 are last
order by case when user_id in (2,3) then 2 else 1 end, user_points desc
Edit: changed id by user_id and corrected outside order by (sorry about that)
On the inner select:
By using this case calculation, what you do is ensuring that records with ids equal to 2 and 3 are "important" (firstly ordered in the order by). Those receive 1 while the others receive 2 as order value, only after that points are relevant.
On the outer select:
Records with ids 2 and 3 recieve 2 as order value, while the rest recieve 1. So they go last irrespective of its "default"
Here you have a reduced fiddle http://sqlfiddle.com/#!9/377c1/1

Two ORDER BY clauses and a LIMIT

This is some sorting: I have a table with ids, names and ratings. I want to list the 10 best items based on ratings, that's fine:
SELECT id FROM items ORDER BY ratings DESC LIMIT 10
But now here comes the hard part: I want to list the 10 best items based on ratings but order them alphabetically. I tried
SELECT id FROM items ORDER BY ratings DESC, names ASC LIMIT 10
and
SELECT id FROM items ORDER BY names ASC, ratings DESC LIMIT 10
but neither gives the desired result.
You could use a subquery:
SELECT *
FROM (
SELECT id, names
FROM items
ORDER BY ratings DESC
LIMIT 10
) t
ORDER BY names
EDIT: Further Explanation
Your original query sorts by ratings DESC, and then by names -- it would only sort the names with the same ratings. So if two items had the same rating, then you'd get those 2 sorted by name. It's going to sort all your records by rating first, and then by name afterwards (within the same ratings).
SELECT id, name, rating
FROM
(SELECT id, name, rating FROM items ORDER BY ratings LIMIT 10) t
ORDER BY name
This will take top 10 by rating, and then sort by name.
Your queries are incorrect because: They sorts rows by names and then by ratings (or in oposite order), taking top10 after the sort is performed. You need to make the first sort, take top 10 and then sort these 10 results using second column values.