•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 361,561 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,041 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 542 | Replies: 3 | Solved
![]() |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
I have multiple columns I wish to order by.
I have columns:
# # # # (1, 2, 3, 4)
A 0 0 0 0
B 1 0 1 3
C 1 0 2 3
I want to ORDER BY 4, 3, 2, 1.
Syntax I am using:
Note: I am not sure wether to use ASC or DESC - or even if I have to use it.
Thanks, Regards X
I have columns:
# # # # (1, 2, 3, 4)
A 0 0 0 0
B 1 0 1 3
C 1 0 2 3
I want to ORDER BY 4, 3, 2, 1.
Syntax I am using:
"SELECT * FROM table ORDER BY 4, 3, 2, 1"
Thanks, Regards X
•
•
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,861
Reputation:
Rep Power: 7
Solved Threads: 205
Order by multiple columns work this way.
For example, you have 4 records.
If your query is,
the result would be,
As you can see, it sorts field2 first. Then it sorts field3 maintaining the order of field2. Then maintaining the order of field2 and field3, it sorts field4.
I know my explanation isn't really good ! (Or is it more confusing ?)
Note: If you don't specify how you want to sort your records, it will sort in ascending order by default.
For example, you have 4 records.
•
•
•
•
field1 | field2 | field3 | field4
-------+-------+--------+----------
1 | 1 | 2 | 4
-------------------------------------
2 | 2 | 100 | 5
-------------------------------------
3 | 2 | 1 | 3
-------------------------------------
4 | 3 | 500 | 6
-----------------------------------
5 | 2 | 1 | 2
If your query is,
php Syntax (Toggle Plain Text)
$query = "select * from table order by field2,field3,field4";
the result would be,
•
•
•
•
field1 | field2 | field3 | field4
-------+-------+--------+----------
1 | 1 | 2 | 4
-------------------------------------
5 | 2 | 1 | 2
---------------------------------------
3 | 2 | 1 | 3
-------------------------------------
2 | 2 | 100 | 5
-------------------------------------
4 | 3 | 500 | 6
-------------------------------------
As you can see, it sorts field2 first. Then it sorts field3 maintaining the order of field2. Then maintaining the order of field2 and field3, it sorts field4.
I know my explanation isn't really good ! (Or is it more confusing ?)
Note: If you don't specify how you want to sort your records, it will sort in ascending order by default.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
Ya thats what I have been trying.
The problem is I keep getting the same result.
That is:
The table comes out perfect BUT
it comes out backwards...
So what should be first is last, second is second last, and so on.
Get what I mean? Im not being confusing now
The thing is even when I put ASC or DESC the table stays the same way...
Ill relate my problem to your table.
I have sorted my table via field4 then field 3 and the output is a backward output.
Dont know why
Thanks nav, once again.
The problem is I keep getting the same result.
That is:
The table comes out perfect BUT
it comes out backwards...
So what should be first is last, second is second last, and so on.
Get what I mean? Im not being confusing now

The thing is even when I put ASC or DESC the table stays the same way...
Ill relate my problem to your table.
I have sorted my table via field4 then field 3 and the output is a backward output.
Dont know why

Thanks nav, once again.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb MySQL Marketplace
Similar Threads
- Pointers (archived tutorial) (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- Multiple Identity Columns In SQL (ASP.NET)
- Open In New Window Php (PHP)
- multiple update with text boxes (PHP)
- Problem Updating Row in GridView (ASP.NET)
- Method to determine highest sum value of several columns (MySQL)
- How do I populate multiple column list box? (Visual Basic 4 / 5 / 6)
- Pointers (C++)
- Long Island Guide (Website Reviews)
Other Threads in the MySQL Forum
- Previous Thread: PHP in MySQL Trigger
- Next Thread: Data from Multiple Tables



Linear Mode