Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 786
user774105 Avatar asked May 31 '11 02:05

user774105


People also ask

How do you write a query for ORDER BY?

Syntax. SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC]; You can use more than one column in the ORDER BY clause.

What is the ORDER BY query?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do you do ORDER BY in SQL?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.


1 Answers

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  
like image 200
a1ex07 Avatar answered Sep 27 '22 18:09

a1ex07