user774105
user774105

Reputation: 101

SQL ORDER BY query

I want to have my table,rcarddet, ordered by "SDNO" (not primary key) in ascending order with the exception of "0". So it should turn out to be like:

1
1 
2
.
.
10
0
0

My query now is:

SELECT * 
  FROM `rcarddet` 
 WHERE `RDATE` = '2011-05-25' 
   AND `RCNO` = '1' 
   AND `PLACE` = 'H' 
   AND `SDNO` != 0 
ORDER BY `rcarddet`.`SDNO` ASC;

Upvotes: 5

Views: 134

Answers (2)

Andriy M
Andriy M

Reputation: 77737

SELECT * 
  FROM `rcarddet` 
 WHERE `RDATE` = '2011-05-25' 
   AND `RCNO` = '1' 
   AND `PLACE` = 'H' 
ORDER BY
  `SDNO` = 0,
  `SDNO`;

Upvotes: 4

a1ex07
a1ex07

Reputation: 37382

The easiest way

  SELECT * FROM rcarddet   
   WHERE RDATE = '2011-05-25' and RCNO = '1'and PLACE = 'H'  
ORDER BY CASE  
           WHEN rcarddet.SDNO  = 0 THEN [max_number_for_the_type_of_SDNO]  
           ELSE rcarddet.SDNO   
         END ASC  

Upvotes: 6

Related Questions