therealwesfoster Posted January 8, 2008 Share Posted January 8, 2008 I have a question, how can I get information and show it in order according to 2 different columns in the db row? Example: MYTABLE: id name number1 number2 I want to get all the rows from MYTABLE, and IF number1 is greater than 0, order them ASC. After all of the rows where number1 is greater than 0 are gone through, start with all the other rows and order them all ASC by number2. I hope im making sense.. how could I do this with 1 query? Link to comment https://forums.phpfreaks.com/topic/85074-ordering-rows-by-2-columns/ Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 SELECT number1, number2 FROM mytable WHERE number1 > 0 ORDER BY number2 ASC Link to comment https://forums.phpfreaks.com/topic/85074-ordering-rows-by-2-columns/#findComment-433847 Share on other sites More sharing options...
therealwesfoster Posted January 8, 2008 Author Share Posted January 8, 2008 I don't think you understood me completely.. thanks for the reply though. I want to display all the rows. By default, number1 is left at 0. But if by chance if number1 is greater than 0, I want it to first grab all of those rows and order them by that number, then only AFTER all of the rows that contain a number greater than 0 in the number1 column are used, grab the rest of the rows and order them by their number (number2). See, all of the rows have a number2, but by having something greater than 0 in the number1 column, I want them to get boosted up to the top of the list. I think i explained it a little better this time Link to comment https://forums.phpfreaks.com/topic/85074-ordering-rows-by-2-columns/#findComment-433852 Share on other sites More sharing options...
Ken2k7 Posted January 8, 2008 Share Posted January 8, 2008 SELECT number1 FROM mytable WHERE number1 = 0 UNION SELECT number1, number2 FROM mytable WHERE number1 > 0 ORDER BY number2 ASC ? Link to comment https://forums.phpfreaks.com/topic/85074-ordering-rows-by-2-columns/#findComment-433859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.