求SQL语句

fujlt 2005-03-17 04:47:56
现在有以下表
T_user:
name nameAsCust CanSee
NJ Nj SZ,WX
SH SH SH,SJ,WGQ

T_car:
cph belongTo
A1234 SJ
A2356 WX
A3546 WGQ

希望可以用登陆名NJ登陆时,使用一条查询语句获取车辆A2356
而用SH登陆的时候,可以查询到车辆A1234,和A3546

即有一变量 ,为SH时
查询结果为:A1234
A3546
如果为NJ时
查询结果为:A2356





...全文
85 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
davytao1018 2005-03-17
  • 打赏
  • 举报
回复
create table #T_user(
name varchar(10), nameAsCust varchar(10), CanSee varchar(200))
insert into #T_user
select
'NJ' , 'Nj' , 'SZ,WX' union all select
'SH' , 'SH' , 'SH,SJ,WGQ'


create table #T_car(
cph varchar(10), belongTo varchar(10))
insert into #T_car
select
'A1234' , 'SJ' union all select
'A2356' , 'WX' union all select
'A3546' , 'WGQ'

select cph from #T_car where charindex(','+belongto+',' , ','+(select cansee from #t_user where name='NJ')+',')>0

drop table #T_car
drop table #T_user
davytao1018 2005-03-17
  • 打赏
  • 举报
回复
select cph from T_car where charindex(','+belongto+',' , ','+(select cansee from t_user where name=变量)+',')>0
Softlee81307 2005-03-17
  • 打赏
  • 举报
回复
select t_car.cph from t_car inner join t_user
on charindex(belongTo,CanSee)>0
where name='Nj' --------這裡可為變量,
xluzhong 2005-03-17
  • 打赏
  • 举报
回复
select a.cph
from t_car a
inner join t_user b
on charindex(a.belongto,b.cansee)>0
where b.name='sh'
Softlee81307 2005-03-17
  • 打赏
  • 举报
回复
up

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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