Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL Order by multiple columns

Tags:

sql

php

mysql

I am looking to create an order based on multiple columns in my table - all date related. For example: my columns are: fee_due, fee_2_due, fee_3_due - they all contains results of various dates. However, I want to create an order combining all columns, so fee_due might be 2012-11-03, fee_2_due might be 2012-11-01 and fee_3_due might be 2012-11-02.

My query needs to be:

SELECT *
FROM table_name
ORDER BY [date] DESC

... whereby the dates from the 3 columns join to form one order, regardless of what column they are in.

Hope that makes sense and thanks in advance.

like image 469
David Avatar asked Nov 30 '22 22:11

David


2 Answers

Additionally you can:

SELECT *
FROM table_name
ORDER BY fee_due ASC,fee_2_due DESC,fee_3_due DESC

You can sort each column independently according to your need.

like image 181
Gutenberg Avatar answered Dec 03 '22 12:12

Gutenberg


I used the following to make it work:

SELECT * FROM table ORDER BY camp1 ASC, camp2 ASC, camp3 DESC
like image 22
MaxHD Avatar answered Dec 03 '22 11:12

MaxHD