SQL 语法错误,求解!!!

z_f_p 2011-06-17 10:47:28
select o.confirmation_no,o.car_no,v.fname,v.vip_card_no,case o.pickup_point when 0 then o.pickup_address else cs.abbr_name end as pickup_address,
case o.dropoff_point when 0 then o.dropoff_address else cs1.abbr_name end as dropoff_address,from_date,to_date,d.fname as pickup_dr_name,d1.fname as dropoff_dr_name,o.status
from operator_self o(nolock)
left outer join vip v(nolock) on o.vip_no=v.vip_no
left outer join car_storehouse cs(nolock) on o.pickup_point=cs.id
left outer join car_storehouse cs1(nolock) on o.dropoff_point=cs1.id
left outer join driver d(nolock) on o.pickup_dr_id=d.dr_id
left outer join driver d1(nolock) on o.dropoff_dr_id=d1.dr_id where 1=1
and (case o.pickup_point when 0 then o.pickup_city in (select city from user_store_right where [user_id]=1)
or case o.dropoff_point when 0 then o.dropoff_city in (select city from user_store_right where [user_id]=1))


报错:消息 156,级别 15,状态 1,第 9 行
Incorrect syntax near the keyword 'in'.
消息 156,级别 15,状态 1,第 10 行
Incorrect syntax near the keyword 'or'.

我用的是 case when 条件筛选, 当o.pickup_point =0时, 筛选条件o.pickup_city
,或者当o.dropoff_point=0时,筛选条件o.dropoff_city 。
大伙看看那里错了。
...全文
808 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
haihongzhu 2011-06-21
  • 打赏
  • 举报
回复
sdfasdfasdfasdf
newdigitime 2011-06-17
  • 打赏
  • 举报
回复


where (o.pickup_point=0 and o.pickup_city in(....) or ( o.dropoff_point =0 and o.dropoff_city in(....) )

当然,用union all也可以.
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssp2009 的回复:]

(case o.pickup_point when 0 then o.pickup_city in (select city from user_store_right where [user_id]=1)
这是什么写法。。
[/Quote]

就是找不到思路,请指点下。
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 newdigitime 的回复:]

引用 9 楼 z_f_p 的回复:

then 后面可以跟表达……

SQL code

不能这样,
你可以用存储过程

if xxx=1
select ... from table where 条件A
else
select .... from table Where 条件B

或者用拼接SQL的办法
declare @sql nvarchar(200)
set @sql=N'se……
[/Quote]

目前有个难题是这个条件是根据查询的列中来的,也就是说,当查询的列o.pickup_point的值为0时筛选条件是。。。。。
当查询的列o.pickup_point不为0时,筛选的条件是。。。。。。
所以我才这样写了。
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 penginpha 的回复:]

忒复杂,不如写个存储过程,效率更高点。
[/Quote]

其实这本来是个存储过程,为了演示代码我把后面的筛选条件省去了点,只剩前面的筛选条件了。还有我把原来存储过程中那个[user_id]=参数变量写成[user_id]=1是为了演示方便。这本来就是个存储过程。
彭金华 2011-06-17
  • 打赏
  • 举报
回复
忒复杂,不如写个存储过程,效率更高点。
newdigitime 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 z_f_p 的回复:]

then 后面可以跟表达……
[/Quote]

不能这样,
你可以用存储过程

if xxx=1
select ... from table where 条件A
else
select .... from table Where 条件B

或者用拼接SQL的办法
declare @sql nvarchar(200)
set @sql=N'select * from tab where '
if xxx=1
set @sql=@sql+'条件A'
else
set @sql=@sql+'条件B'
EXECUTE sp_executesql @Sql


z_f_p 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssp2009 的回复:]
(case o.pickup_point when 0 then o.pickup_city in (select city from user_store_right where [user_id]=1)
这是什么写法。。
[/Quote]
then 后后面我想写个子查询,但是语法好像不行?怎么解决???
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 haojuntu 的回复:]
这样试试。。
where 1=1
and (case o.pickup_point when 0 then o.pickup_city else in (select city from user_store_right where [user_id]=1)
or case o.dropoff_point when 0 then o.dropoff_city else in (s……
[/Quote]

这样不行,之前都试过了。
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 newdigitime 的回复:]
case when主要用于根据不同条件返回不同的值.
你不能用在"根据不同的条件返回一个不同的SQL条件语句(以用于执行)"


case when State=1 and Joinstate=0 then 2 when State=1 end

另外
case when 最后还要有end结束符.
[/Quote]

then 后面可以跟表达式吗?我想实现的效果是当o.pickup_point =0执行一个子查询!
newdigitime 2011-06-17
  • 打赏
  • 举报
回复
case when主要用于根据不同条件返回不同的值.
你不能用在"根据不同的条件返回一个不同的SQL条件语句(以用于执行)"


case when State=1 and Joinstate=0 then 2 when State=1 end

另外
case when 最后还要有end结束符.
小童 2011-06-17
  • 打赏
  • 举报
回复
haojuntu 2011-06-17
  • 打赏
  • 举报
回复
这样试试。。
where 1=1
and (case o.pickup_point when 0 then o.pickup_city else in (select city from user_store_right where [user_id]=1)
or case o.dropoff_point when 0 then o.dropoff_city else in (select city from user_store_right where [user_id]=1))
快溜 2011-06-17
  • 打赏
  • 举报
回复
(case o.pickup_point when 0 then o.pickup_city in (select city from user_store_right where [user_id]=1)
这是什么写法。。
begintransaction 2011-06-17
  • 打赏
  • 举报
回复
这个SQL语句我实在没耐心看下去。
  • 打赏
  • 举报
回复
太长了。。。。。



去SQL版吧
md5e 2011-06-17
  • 打赏
  • 举报
回复
写得太乱,看得有点晕
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
木有人????
quanhj 2011-06-17
  • 打赏
  • 举报
回复
你应该把where 左边的写在case when 的外面,不应该写在里面
z_f_p 2011-06-17
  • 打赏
  • 举报
回复
谢谢大家,结贴给分了!
加载更多回复(5)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧