Ordering SQL query by specific field values

血红的双手。 提交于 2019-12-08 15:53:44

问题


I've got a sql query (using Firebird as the RDBMS) in which I need to order the results by a field, EDITION. I need to order by the contents of the field, however. i.e. "NE" goes first, "OE" goes second, "OP" goes third, and blanks go last. Unfortunately, I don't have a clue how this could be accomplished. All I've ever done is ORDER BY [FIELD] ASC/DESC and nothing else.

Any suggestions?

Edit: I really should clarify: I was just hoping to learn more here. I have it now that I just have multiple select statements defining which to show first. The query is rather large and I was really hoping to learn possibly a more effecient way of doing this: example:

SELECT * FROM RETAIL WHERE MTITLE LIKE 'somethi%' AND EDITION='NE'
UNION 
SELECT * FROM RETAIL WHERE MTITLE LIKE 'somethi%' AND EDITION='OE'
UNION
SELECT * FROM RETAIL WHERE MTITLE LIKE 'somethi%' AND EDITION='OP'
UNION (etc...)

回答1:


Order By Case Edition
    When 'NE' Then 1
    When 'OE' Then 2
    When 'OP' Then 3
    Else 4 End 



回答2:


SELECT 
  /*other fields*/
  CASE WHEN 'NE' THEN 1
    WHEN "OE" THEN 2
    WHEN "OP" THEN 3
    ELSE 4
END AS OrderBy
FROM
  /*Tables*/
WHERE
  /*conditions*/
ORDER BY
  OrderBy,
  /*other fields*/



回答3:


Add those values to another table with a numeric column for their rank:

Edition  Rank
NE       1
OE       2
OP       3

Join the tables, and sort on the RANK field.




回答4:


Try:

select *
from MyTable
order by
case [FIELD] 
    when 'NE' then 1
    when 'OE' then 2
    when 'OP' then 3
    when '' then 4
    else 5
end



回答5:


Try this:

ORDER BY FIND_IN_SET(EDITION, 'NE,OE,OP,')



回答6:


How about this?

SELECT * 
FROM RETAIL
WHERE MTITLE LIKE 'somethi%'
ORDER BY POSITION(EDITION, ' OP OE NE') DESC

If substr is empty string, the result is 1. If no match is found, the result is 0.

Position()

Added in Firebird 2.1

reference: https://firebirdsql.org/refdocs/langrefupd21-intfunc-position.html




回答7:


    CREATE TABLE #TMP
(
       ID INT IDENTITY(1,1),
       NAME VARCHAR(100),
)

INSERT INTO #TMP
SELECT 'ASHISH'
UNION ALL
SELECT 'CHANDAN'
UNION ALL
SELECT 'DILIP'
UNION ALL
SELECT 'ESHA'
UNION ALL
SELECT 'FIZA'
UNION ALL
SELECT 'MAHESH'
UNION ALL
SELECT 'VIPUL'
UNION ALL
SELECT 'ANIL'

-- I want to sort NAME column from value 'DILIP' then query will be as bellow

SELECT * FROM #TMP ORDER BY CASE WHEN NAME='DILIP' THEN '1' ELSE NAME END ASC

DROP TABLE #TMP



回答8:


SELECT * FROM (

SELECT 1 as Rank, R.* FROM RETAIL R WHERE MTITLE LIKE 'somethi%' AND EDITION='NE'
   UNION ALL
SELECT 2 as Rank, R.* FROM RETAIL R WHERE MTITLE LIKE 'somethi%' AND EDITION='OE'
   UNION ALL
SELECT 3 as Rank, R.* FROM RETAIL R WHERE MTITLE LIKE 'somethi%' AND EDITION='OP'
   UNION ALL (etc...)

) ORDER BY 1



回答9:


SELECT (CASE WHEN 'NE' THEN 1
    WHEN "OE" THEN 2
    WHEN "OP" THEN 3
    ELSE 4) as orden,* FROM Retail WHERE MTITLE LIKE 'somethi%' 
     AND EDITION IN ('NE', 'OE', 'OP', '') ORDER BY Orden


来源:https://stackoverflow.com/questions/398468/ordering-sql-query-by-specific-field-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!