i am using MYSQL and i would like to "SELECT' a table and sort according to a numerical value that i either get it from "column1" or "column2", so:
***if a number from column1 exist, then this number is used to be sorted, otherwise the number from column2 (which always exists) is the number used to be sorted***
this is my current SQL statement:
PHP Code:
SELECT *
FROM table
WHERE regtime >=DATE_SUB(NOW(), INTERVAL 20 DAY)
ORDER BY regtime ASC
i think i should use "CASE" but what i can do now is only sort in "column1" first if column1 IS NOT NULL, THEN sort in "column2"
i appreciate your help as this is already one of my last stages on my personal website
SELECT *, IFNULL(column_1, column_2) AS sort_by_this
FROM table_name . . . ORDER BY sort_by_this
Last edited by NogDog; 12-09-2012 at 05:37 PM.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks