Sql '处的语法不正确=';

Sql '处的语法不正确=';,sql,sql-server,Sql,Sql Server,我正在尝试做类似的事情。我有这张桌子: tab_id是第二列订单”选项卡中的“代码>是第四列。”> tab\u id是第二列。order\u in\u tab是第四列 我想先按tab\u id等于2的顺序排序,然后按tab\u id的其余部分升序排序,然后按order\u在tab升序排序 select * from cam_to_tab_mapping where unit_id='90013550' order by (tab_id='2') asc, tab_id asc, order

我正在尝试做类似的事情。我有这张桌子:

tab_id是第二列<“代码>订单”选项卡中的“代码>是第四列。”>

tab\u id
是第二列。
order\u in\u tab
是第四列

我想先按
tab\u id
等于
2
的顺序排序,然后按
tab\u id
的其余部分升序排序,然后按
order\u在
tab
升序排序

select * 
from cam_to_tab_mapping 
where unit_id='90013550' 
order by (tab_id='2') asc, tab_id asc, order_in_tab asc

但是,它在“=”处表示
不正确的语法。
。我是一个完全的SQL新手,所以我不确定是什么错了(或者如果我误解了上面的链接解决方案)。

尝试如下更改查询:

select * 
from cam_to_tab_mapping 
where unit_id='90013550' 
order by CASE WHEN tab_id='2' THEN 1 ELSE 0 END DESC, tab_id asc, order_in_tab asc

尝试按以下方式更改查询:

select * 
from cam_to_tab_mapping 
where unit_id='90013550' 
order by CASE WHEN tab_id='2' THEN 1 ELSE 0 END DESC, tab_id asc, order_in_tab asc

我认为您的查询中存在复制粘贴错误

select * 
from cam_to_tab_mapping 
where unit_id='90013550' 
order by (tab_id='2') asc, tab_id asc, order_in_tab asc
您有一个逻辑表达式作为第一个
orderby
条件

也许你是说

select * 
from cam_to_tab_mapping 
where unit_id='90013550' and tab_id='2'
order by tab_id asc, order_in_tab asc

我认为您的查询中存在复制粘贴错误

select * 
from cam_to_tab_mapping 
where unit_id='90013550' 
order by (tab_id='2') asc, tab_id asc, order_in_tab asc
您有一个逻辑表达式作为第一个
orderby
条件

也许你是说

select * 
from cam_to_tab_mapping 
where unit_id='90013550' and tab_id='2'
order by tab_id asc, order_in_tab asc

在order by中,您不能指定tab_id=2。您必须添加另一个虚拟字段,该字段的tab_id=2为0,否则为1,并首先按该字段排序,然后按tab_id。请尝试这种方式

select mac, tab_id, unit_id, order_in_tab, 
(case when tab_id='2' then 0 else 1 end) as temp
from cam_to_tab_mapping 
where unit_id='90013550' 
order by temp, tab_id, order_in_tab

您(可以但)不需要在order by子句中指定asc,如果您不指定任何内容,则默认为asc。

在order by中,您不能指定tab_id=2。您必须添加另一个虚拟字段,该字段的tab_id=2为0,否则为1,并首先按该字段排序,然后按tab_id排序。尝试这种方法

select mac, tab_id, unit_id, order_in_tab, 
(case when tab_id='2' then 0 else 1 end) as temp
from cam_to_tab_mapping 
where unit_id='90013550' 
order by temp, tab_id, order_in_tab

您(可以但)不需要在order by子句中指定asc,如果您没有指定任何内容,则默认为asc。

您链接的问题是正确的。您只是误解了字段的类型。它有一个字符串字段,可以与字符串相等

因此,在您的情况下,您必须这样做:

select * 
 from cam_to_tab_mapping 
where unit_id='90013550' 
order by (tab_id=2) DESC, 
        tab_id asc, order_in_tab asc
(tab_id=2)DESC
将在结果中首先显示带有2的id

在小提琴上看到它:

编辑:

OP说它使用的是SQL Server。这个答案是针对MySQL的。在SQL Server上,正确的方法是使用如下CASE语句:

select * 
 from cam_to_tab_mapping 
where unit_id='90013550' 
order by (case when tab_id=2 then 0 else 1 end), 
        tab_id, order_in_tab

你链接的问题是对的。你只是误解了字段的类型。它有一个字符串字段,可以与字符串相等

因此,在您的情况下,您必须这样做:

select * 
 from cam_to_tab_mapping 
where unit_id='90013550' 
order by (tab_id=2) DESC, 
        tab_id asc, order_in_tab asc
(tab_id=2)DESC
将在结果中首先显示带有2的id

在小提琴上看到它:

编辑:

OP说它使用的是SQL Server。这个答案是针对MySQL的。在SQL Server上,正确的方法是使用如下CASE语句:

select * 
 from cam_to_tab_mapping 
where unit_id='90013550' 
order by (case when tab_id=2 then 0 else 1 end), 
        tab_id, order_in_tab


确定这个“order by(tab_id='2')”可以工作吗?到目前为止,在order by中从未见过“=”,你在使用哪个数据库?你想用这个
(tab_id='2')
实现什么?我犯了同样的错误,OP post说,“我想先按tab_id等于2的顺序排序,然后tab_id的其余部分升序,然后tab_tab升序。”。“请参阅我的答案@controlnetictwerkguroorc,它显示了您误解的内容。确定此“订购人(tab_id='2')”可以工作吗?到目前为止,还没有在order by中看到“=”,你在使用哪个数据库?你想用这个
(tab_id='2')
实现什么?我犯了同样的错误,OP post说,“我想先按tab_id等于2的顺序,然后tab_id的其余部分升序,然后tab_in_tab升序。”,它显示了你误解了什么。关闭。。。ASC不应该是DESC吗,因为他先要这些?或者只需在case语句中切换1和0。您能否详细解释一下tab_id='2'然后1 ELSE 0 END DESC时的
情况?此语句表示计算值。如果tab_id='2',则计算值为1,否则为0。然后根据这个计算值进行排序我认为“按案例排序,当tab_id='2'然后-1 ELSE tab_id结束ASC,order_in_tab ASC”是他要求的解决方案。@dotnetom不会将0放在1之前(指
然后1 ELSE 0
部分)?或者SQL不是那样工作的?关闭。。。ASC不应该是DESC吗,因为他先要这些?或者只需在case语句中切换1和0。您能否详细解释一下tab_id='2'然后1 ELSE 0 END DESC时的
情况?此语句表示计算值。如果tab_id='2',则计算值为1,否则为0。然后根据这个计算值进行排序我认为“按案例排序,当tab_id='2'然后-1 ELSE tab_id结束ASC,order_in_tab ASC”是他要求的解决方案。@dotnetom不会将0放在1之前(指
然后1 ELSE 0
部分)?或者SQL不是这样工作的?我犯了同样的错误,OP post说,“我想先按tab_id等于2的顺序排序,然后tab_id的其余部分升序,然后tab_id的顺序升序。”我犯了同样的错误OP post说,“我想先按tab_id等于2的顺序排序,然后tab_id的其余部分升序,然后tab_id的顺序升序。”你可以这样做。看我的答案,你能做到的。看看我的答案。它还在抱怨他
=
sign@CyberneticTwerkGuruOrc那么,您的关系数据库管理系统是什么?Mysql、Oracle、Postgre MSSql?我是通过Microsoft SQL Server管理研究来完成这项工作的,而您不能这样做。这是一个Mysql语句。您应该使用@dotnetom提供的答案,因为sql server不支持此功能。它仍在抱怨他
=
sign@CyberneticTwerkGuruOrc那么,您的关系数据库管理系统是什么?Mysql、Oracle、Postgre MSSql?我是通过Microsoft SQL Server管理研究来完成这项工作的,而您不能这样做。这是一个Mysql语句。您应该使用@dotnetom提供的答案,因为sql server不支持此选项。