I have a table like that I want to take the 20 most hit singers and order them (these 20 singers) alphabeticly.
id name hit
----------------
1 Beyonce 2540
2 Eminem 1432
3 Pink 1642
4 Shakira 1234
.
.
For example I use this code
$query = mysql_query("SELECT * FROM pm_categories ORDER BY hit DESC limit 20");
I take the 20 with the most hits, but I want to also order them alphabeticly. How can I do that?
This should do it:
SELECT *
FROM pm_categories
WHERE id IN (
SELECT id
FROM pm_categories
ORDER BY hit DESC
LIMIT 20
) ORDER BY name
You need to query based on the hits separately, and then use the ids of the top 20 to query and sort by name.
Since you have an older version of mysql, you will need to do a join instead.
SELECT cat.*
FROM (
SELECT id
FROM pm_categories
ORDER BY hit DESC
LIMIT 20
) top_hits
JOIN pm_categories cat ON top_hits.id = cat.id
ORDER BY cat.name
Try
$query = mysql_query("SELECT * FROM pm_categories ORDER BY hit DESC, name ASC limit 20");