Log in

View Full Version : Sort rows, does if / else work in select?



Dennis_Gull
04-27-2007, 02:21 PM
Hello I got a problem when Im going to sort one of the tables I have.
If one of the the tables column is current_number > 0 I want to sort it by the current number but if the column is current_number < 1 I want to sort it by the start_number but when I select the tables I cant get the if or else to work.. it looks like this now:



$listing_sql = "select ..... some stuff and..... IF(s.status, s.specials_status, p.current_number) as final_number.

The IF statement is still in the Select section so Is it possible to place an else or something like that? final_number is the one Im later on use sort desc / asc with.

hmm to clear this up a bit, lets say I have those four numbers in the two columns:
10 - start_number (this one have current_number 0)
15 - current_number
20 - start_number (this one have current_number 0)
25 - current_numbers

now I want to sort them with a button called "Sort number", The button is using the name final_number that I made when I selected the tables (see the html code). Is it even possible to mix them and then sort them.

Basically I wanna know if its possible to use two different columns as the same Alias but during certain circumstances.

Hope you understand what im talking about :)

Dennis_Gull
04-27-2007, 06:10 PM
I fixed it by adding a new column but Im still interested if theres another solution.

Twey
04-27-2007, 06:22 PM
SELECT x FROM y ORDER BY IF(current_number < 1, start_number, current_number);... surely.

Dennis_Gull
04-27-2007, 07:02 PM
thank you. :)