我有一个SQL查询(使用Firebird作为RDBMS),我需要通过字段来订购结果,版本.但是,我需要按字段的内容排序.即"NE"第一,"OE"第二,"OP"第三,空白最后.不幸的是,我不知道如何实现这一目标.我所做过的只是ORDER BY [FIELD] ASC/DESC而已.
有什么建议?
编辑:我真的应该澄清:我只是希望在这里了解更多.我现在拥有它,我只有多个select语句定义哪个首先显示.查询相当大,我真的希望学习一种更有效的方法:例如:
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...)
Charles Bret.. 52
Order By Case Edition When 'NE' Then 1 When 'OE' Then 2 When 'OP' Then 3 Else 4 End
Pulsehead.. 6
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*/
Peter T. LaC.. 5
将这些值添加到另一个表中,其中包含排名的数字列:
Edition Rank NE 1 OE 2 OP 3
加入表格,并在RANK字段上排序.
Order By Case Edition When 'NE' Then 1 When 'OE' Then 2 When 'OP' Then 3 Else 4 End
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*/
将这些值添加到另一个表中,其中包含排名的数字列:
Edition Rank NE 1 OE 2 OP 3
加入表格,并在RANK字段上排序.