开发者

SQL: query that fetches the rows ordered by the value of a column and fetches first the rows with a concret value in that column

开发者 https://www.devze.com 2023-01-24 12:53 出处:网络
I want to do a query that fetchs the rows ordered by the value of a column and in the same time i want it fetches first the rows that has a concret value in that column.

I want to do a query that fetchs the rows ordered by the value of a column and in the same time i want it fetches first the rows that has a concret value in that column.

For example:

it
es
开发者_Go百科fr
it
es
fr

If i want to show the rows with "es" first of all, the result would be:

es
es
it
it
fr
fr 

Regards

Javi


I am guessing here you want to get first the records with 'es' in the column and afterwards the rest in ascendent order (in your example you are using descendent order).

If that is the case you can do it like this (in Oracle)

Select *
From
(
  Select T.*, 0 orden
    From Table T
   Where column_name= 'es'
   Union
  Select T.*, 1 orden
    From Table T
   Where column_name <> 'es'
)
Order By orden, column_name

To change the order direction for the column just put a Desc in the order by clause.

EDIT: for MySQL try instead

Select *
From
(
  Select T.*, 0 orden
    From Table AS T
   Where column_name= 'es'
   Union
  Select T.*, 1 orden
    From Table AS T
   Where column_name <> 'es'
)
Order By orden, column_name

I have no MySQL instance at hand now so I can not test it.

Best regards

0

精彩评论

暂无评论...
验证码 换一张
取 消