返回具有特定值的行首先获得语法错误

时间:2014-06-30 12:33:52

标签: sql sql-server

我希望我的查询返回表中的行,其中列首先包含特定值

所以我给出了这样的查询:

SELECT IDName
  FROM Type_ofID_tbl
 WHERE deleted = 0
 ORDER BY(IDName ='Emirates ID'), IDName

但显示错误

' ='。

附近的语法不正确

3 个答案:

答案 0 :(得分:1)

(IDName ='Emirates ID')条款中不需要ORDER BY;它应该是这样的

 SELECT IDName
   FROM Type_ofID_tbl
  WHERE deleted = 0
  ORDER BY IDName ASC

如果您想订购具有IDName ='Emirates ID'

的列
 SELECT IDName
   FROM Type_ofID_tbl
  WHERE deleted = 0
    AND IDName = 'Emirates ID'
  ORDER BY IDName ASC

ORDER BY关键字默认按升序对记录进行排序。要按降序对记录进行排序,您可以使用DESC关键字。

答案 1 :(得分:0)

试试这个

select IDName 
from Type_ofID_tbl 
where deleted=0 order by case when IDName ='Emirates ID' then 1 else 2 end

答案 2 :(得分:0)

除非明确说明,否则SQL表排序是任意的。此外,您不能在order by子句中使用逻辑运算符。

我的建议是将其分为两个查询:

SELECT IDNAME FROM [TABLE]
WHERE DELETED = 0
AND IDNAME = 'EMIRATES ID'
UNION ALL
SELECT IDNAME FROM [TABLE]
WHERE DELETED = 0
AND IDNAME != 'EMIRATES ID'
OR IDNAME IS NULL

感谢。