[MySql] Order from Highest to Lowest Number [RESOLVED]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

[MySql] Order from Highest to Lowest Number [RESOLVED]

Post by spec36 »

Hi guys,

I have a PHP that queries a database, counts specific errors and then outputs a count of all errors to the screen, but this list is not ordered from Highest to lowest count on the output.

I use this code to count the amount of each error from the DB.

Code: Select all

$query = "select '4444' as 'error', count(*) as 'total' from table where desc like '%error: 4444%'
union select '5555' as 'error', count(*) as 'total' from logs where desc like '%error: 5555%'
union select '6666' as 'error', count(*) as 'total' from logs where desc like '%error: 6666%'
Can anyone help me to add the Mysql command that will sort the error count from highest to lowest?

Example:
If total errors were -> Error 4444 = 30, Error 5555 = 60, Error 6666 = 3. I want the output to look like this:

Error 5555 = 60
Error 4444 = 30
Error 6666 = 3

Currently it looks like this:

Error 4444 = 30
Error 5555 = 60
Error 6666 = 3

Any help would be greatly appreciated.

Thanks
Last edited by spec36 on Mon Jul 12, 2010 3:09 pm, edited 2 times in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: [MySql] Order from Highest to Lowest Number

Post by AbraCadaver »

You could try adding an ORDER BY total DESC to the end.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Re: [MySql] Order from Highest to Lowest Number

Post by spec36 »

What is the correct syntax to add "order by total desc" to the below? I am using Mysql 5.1.45

I keep getting this error

Code: Select all

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc union select '4444' as 'error', count(*) as 'total' from table ' at line 1
I changed the code to this:

Code: Select all

$query  = "select '4444' as 'error', count(*) as 'total' from table where desc like '%error: 4444%',  order by total desc
union select '5555' as 'error', count(*) as 'total' from logs where desc like '%error: 5555%', order by total desc
union select '6666' as 'error', count(*) as 'total' from logs where desc like '%error: 6666%', order by total desc'";
Note: If I take this out of the mysql code, it works as before without the sorting.

Code: Select all

, order by total desc'
Thanks for your time.
Last edited by spec36 on Mon Jul 12, 2010 3:09 pm, edited 1 time in total.
spec36
Forum Commoner
Posts: 28
Joined: Thu Nov 19, 2009 6:07 pm

Re: [MySql] Order from Highest to Lowest Number

Post by spec36 »

I figure this out by adding this to the last line of my mysql query:

Code: Select all

order by total DESC ";
Thanks for the help
Post Reply