tags:

views:

40

answers:

3

I was wondering how can I order my MySQL data using the DESC and ASC in my following code I tried doing this but some how it did not work can someone help me fix this problem.

Here is the code.

$result = mysql_query("SELECT a_tags.*, tags.* 
    FROM aa_tags 
    INNER JOIN tags ON tags.id = a_tags.tag_id 
    WHERE a_tags.users_a_id=3");
+1  A: 

You would just add this to the end of the query:

ORDER BY field_you_want_to_sort_by ASC

or

ORDER BY field_you_want_to_sort_by DESC

Is that what you tried? If so, give us the exact query and tell us the problem you had.

Scott Saunders
yeah I tried that and it didn't work.
myhut
Give us the exact query you ran and tell us what "it didn't work" means.
Scott Saunders
I kind of fixed it on my own but another problem came up. Thanks though.
myhut
A: 
$result = mysql_query("SELECT a_tags.*, tags.* 
                                           FROM aa_tags 
                                           INNER JOIN tags ON tags.id = a_tags.tag_id 
                                           WHERE a_tags.users_a_id=3
                                           ORDER BY fieldname DESC");

Change fieldname to your fieldname.

Gazler
A: 

What datatypes are these fields you are sorting by? If they are CHAR or VARCHAR you may want to check the COLLATION. If they are binary types but sorting will sort by binary probably won't be what useful.

The ORDER BY clause sort strings by their COLLATION. A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set.

http://dev.mysql.com/doc/refman/5.1/en/charset-general.html

To check the collation you can use:

SHOW CREATE TABLE tablename;
Yada