Two ORDER BY clauses and a LIMIT - mysql

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.

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;

I want to count and group a subset of a table

I want to select the latest 100 rows and then count and group the result, instead of counting and grouping the whole table, but I can't figure out how to do it.
An example of how it might work (but this does not work):
SELECT federal_party, count(federal_party)
From (
SELECT federal_party
FROM database
ORDER BY date_updated DESC LIMIT 100
)
GROUP BY federal_party
Any idea how this would best be done?

MySQL - Order by one column for a limit then by another column for a limit

I'm developing an application that displays rows which have columns "Time created" and "Number of Likes".
I'm trying to return the first 5 results of my query with the rows that have the most likes, and then the remaining 95 by most recent date, without duplication.
My current query only accomplishes ordering by date.
"SELECT * FROM `$category` ORDER BY time DESC LIMIT 100"
Is what I'm attempting to do possible in one query? Or will I have to perform two queries and filter out duplicate rows?
UNION automatically removes duplicate rows and LIMIT outside the parethesis applies to the whole query.
(SELECT *, like_count AS likes FROM category ORDER BY like_count DESC LIMIT 5)
UNION
(SELECT *, 0 AS likes FROM category ORDER BY time DESC)
ORDER BY likes DESC, time DESC
LIMIT 100

MySQL - Getting Top n records and ordering by a specific column with pagination

I have got a problem with getting top n records from database and ordering them by a specific column and paginating them.
For example I want first 100 movies from movie table and order these first 100 records by name and display 10 records per page.
However this doesn't work;
SELECT name FROM movies ORDER BY id DESC, name DESC, LIMIT 0,10
I am quite confused here. In order to paginate I have to use LIMIT in such ways;
LIMIT 0,10 = FIRST PAGE
LIMIT 10,20 = SECOND PAGE
and so on.
In order to get first records, I use ORDER BY id DESC however when I want to list FROM Z to A, ORDER BY id DESC, name DESC doesn't do the trick.
In another words what I want to do is to get first (latest) 100 records out of 10.000 and order this 100 records by name (ASC or DESC) and / or by view (ASC or DESC).
I hope I was clear enough to explain my problem.
I will be glad if you could help me out with this one.
Shift the order of your order by statements
(updated)
select name
from ( select *
from movies
order by id desc
limit 100 )
order by name desc
limit 0,10
It uses the first one first and if equal it looks at the next one
You could try to use
select name from
(select name from movies order by id desc limit 0,100)
order by name desc limit 0,10
Wrap your select in another select;
SELECT name FROM (SELECT * FROM movies ORDER BY id DESC LIMIT 0,100) ORDER BY name DESC LIMIT 0,10
Edit: Updated limits.

Query double sort

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;