SQL中exists的用法

wusenet041 2009-03-12 02:59:30
表A table :jyb
XNO(购买人的ID) IDKEY(交易号) (KF_code)购买地点ID JSRID(服务人员ID)
00001 001 0001 admin
00002 002 0002 011051
00001 003 0001 admin
00002 004 0001 011021
00002 005 0002 011031
00001 006 0002 011031
........
........

表B 权限表
JSR_ID KF_CODE
admin 0001
admin 0002
011051 0002
011021 0001
011031 0002
我想写个SQL语句 当查询的人员是admin的时候 就能查询出0001仓库和0002仓库交易的所有记录
查询011051的时候 就只能查询出0002仓库的记录 好像是用exists语句 但是不知道怎么写 高手帮帮忙啊
...全文
1446 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjjssh 2010-04-21
  • 打赏
  • 举报
回复
我是新手
select jyb.* from jyb
where kf_id in(0001,0002) and exits(select * from 权限表 where jsr_id='admin' and kf_id in(0001,0002))

select jyb.* from jyb
where kf_id in(0002) and exits(select * from 权限表 where jsr_id='011051' and kf_id in(0002))
因为权限表  有可能存在:
admin 0003
admin 0004
011051 0005
011051 0006
百年树人 2009-03-12
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[jyb]') is not null drop table [jyb]
go
create table [jyb]([XNO] varchar(5),[IDKEY] varchar(3),[KF_code] varchar(4),[JSRID] varchar(6))
insert [jyb]
select '00001','001','0001','admin' union all
select '00002','002','0002','011051' union all
select '00001','003','0001','admin' union all
select '00002','004','0001','011021' union all
select '00002','005','0002','011031' union all
select '00001','006','0002','011031'
if object_id('[权限表]') is not null drop table [权限表]
go
create table [权限表]([JSR_ID] varchar(6),[KF_CODE] varchar(4))
insert [权限表]
select 'admin','0001' union all
select 'admin','0002' union all
select '011051','0002' union all
select '011021','0001' union all
select '011031','0002'

---查询---
select *
from jyb
where
KF_code in(
select
KF_CODE
from 权限表
where JSR_ID='admin'
)

select *
from jyb
where
KF_code in(
select
KF_CODE
from 权限表
where JSR_ID='011051'
)

---结果---
/**
XNO IDKEY KF_code JSRID
----- ----- ------- ------
00001 001 0001 admin
00002 002 0002 011051
00001 003 0001 admin
00002 004 0001 011021
00002 005 0002 011031
00001 006 0002 011031

(所影响的行数为 6 行)
**/
/**
XNO IDKEY KF_code JSRID
----- ----- ------- ------
00002 002 0002 011051
00002 005 0002 011031
00001 006 0002 011031

(所影响的行数为 3 行)
**/
ermuzi 2009-03-12
  • 打赏
  • 举报
回复
大虾们手好快哦~~~
yygyogfny 2009-03-12
  • 打赏
  • 举报
回复
大虾们手好快哦~~~
yygyogfny 2009-03-12
  • 打赏
  • 举报
回复

表A table :jyb
XNO(购买人的ID) IDKEY(交易号) (KF_code)购买地点ID JSRID(服务人员ID)
00001 001 0001 admin
00002 002 0002 011051
00001 003 0001 admin
00002 004 0001 011021
00002 005 0002 011031
00001 006 0002 011031
........
........

表B 权限表
JSR_ID KF_CODE
admin 0001
admin 0002
011051 0002
011021 0001
011031 0002


create table ta
(XNO varchar(10),
IDKEY varchar(10),
KF_code varchar(10),
JSRID varchar(10)
)
insert into ta
select '00001', '001', '0001', 'admin'
union all
select '00002', '002', '0002', '011051'
union all
select '00001', '003', '0001', 'admin'
union all
select '00002', '004', '0001', '011021'
union all
select '00002', '005', '0002', '011031'
union all
select '00001', '006', '0002', '011031'

create table tb
(
JSR_ID varchar(10),
KF_CODE varchar(10)
)
insert into tb
select 'admin', '0001'
union all
select 'admin', '0002'
union all
select '011051', '0002'
union all
select '011021', '0001'
union all
select '011031', '0002'

select * from ta A where jsrid = '011051' and kf_code in (select kf_code from tb B where jsr_id = A.jsrid)
==============================
XNO IDKEY KF_code JSRID
---------- ---------- ---------- ----------
00002 002 0002 011051

(1 row(s) affected)

==============================

select * from ta A where jsrid = 'admin' and kf_code in (select kf_code from tb B where jsr_id = A.jsrid)
==============================
XNO IDKEY KF_code JSRID
---------- ---------- ---------- ----------
00001 001 0001 admin
00001 003 0001 admin

(2 row(s) affected)



肥龙上天 2009-03-12
  • 打赏
  • 举报
回复
select * from 表A a join 表B b on a.KF_CODE = b.KF_CODE where a.JSR_ID ='admin'
dawugui 2009-03-12
  • 打赏
  • 举报
回复
create table A(JSRID varchar(10))
insert into a values('admin')
insert into a values('011051')
insert into a values('admin')
insert into a values('011021')
insert into a values('011031')
insert into a values('011031')
create table B(JSR_ID varchar(10),KF_CODE varchar(10))
insert into b values('admin' , '0001')
insert into b values('admin' , '0002')
insert into b values('011051', '0002')
insert into b values('011021', '0001')
insert into b values('011031', '0002')
go

declare @jsrid as varchar(10)
set @jsrid = 'admin'

select distinct b.* from a , b where a.jsrid = b.jsr_id and a.jsrid = @jsrid
/*
JSR_ID KF_CODE
---------- ----------
admin 0001
admin 0002

(所影响的行数为 2 行)
*/

set @jsrid = '011051'
select distinct b.* from a , b where a.jsrid = b.jsr_id and a.jsrid = @jsrid
/*
JSR_ID KF_CODE
---------- ----------
011051 0002

(所影响的行数为 1 行)
*/

drop table a , b
肥龙上天 2009-03-12
  • 打赏
  • 举报
回复

这一个没有使用到code 啊
直接链表就OK了
百年树人 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 josy 的回复:]
SQL codeselect *
from jyb
where
jsrid in(
select
KF_CODE
from 权限表
where JSR_ID='admin'
)
[/Quote]
更正一个字段
select * 
from jyb
where
KF_code in(
select
KF_CODE
from 权限表
where JSR_ID='admin'
)
dawugui 2009-03-12
  • 打赏
  • 举报
回复
--用变量.

declare @jsrid as varchar(10)
set @jsrid = 'admin'

select b.* from a , b where a.jsrid = b.jsr_id and a.jsrid = @jsrid

set @jsrid = '011051'

select b.* from a , b where a.jsrid = b.jsr_id and a.jsrid = @jsrid
百年树人 2009-03-12
  • 打赏
  • 举报
回复
select * 
from jyb
where
jsrid in(
select
KF_CODE
from 权限表
where JSR_ID='admin'
)
yygyogfny 2009-03-12
  • 打赏
  • 举报
回复
select * from 表A A where jsrid = '查询用户的ID' and kf_code in (select kf_code from 表B B where jsr_id = A.jsrid)
代码转载自:https://pan.quark.cn/s/29a7d91aa8b3 括号内的子查询SQL语句返回结果非空(即:SQL返回的结果为真),当子查询的结果非空这一条件成立时,执行主SQL语句,否则主SQL语句不执行。与EXISTS相对的是NOT EXISTS,括号内的子查询SQL语句返回结果为空(即:SQL不返回的结果为真),子查询的结果为空则条件成立,执行主SQL,否则主SQL不执行。总结:EXISTS和NOT EXISTS语句主要关注是否返回结果集,并不要求了解返回的具体内容,与IN的主要区别在于IN通常只能返回单个字段值,而EXISTS允许返回多个字段值。提醒:文档提供了EXISTS和NOT EXISTS的常用示例,这些示例已经经过本人实际测试,并且文档包含了相关的测试数据SQL用法SQL。在SQL查询EXISTS和NOT EXISTS是两个极为重要的子查询操作符,它们主要用于判断子查询是否能够返回结果集。这篇文章将详细阐述这两个关键字的用法,并通过具体实例进行深入解析。EXISTS的语法结构是:在主查询的条件设置为EXISTS(子查询),如果子查询返回任意行(无论只有一行还是零行),那么EXISTS为真,从而使得整个查询得以继续执行。相对地,如果子查询没有返回任何行,EXISTS为假,主查询将不会被执行。相反地,NOT EXISTS的语法是:在主查询的条件设置为NOT EXISTS(子查询),如果子查询没有返回任何行,NOT EXISTS为真,主查询将被执行;如果子查询返回至少一行,NOT EXISTS为假,主查询将不会被执行。EXISTS和IN之间存在显著差异,IN一般用于比较单个字段值,而EXISTS能够处理更为复杂的查询...

22,297

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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