+ -
当前位置:首页 → 问答吧 → 求助一道SQL问题

求助一道SQL问题

时间:2011-12-14

来源:互联网

表A LPZ03(起始编号) LPZ04(结束编号)
  100010001 100010010
  100010099 100010150
  100011100 100011110
  100011150 100011190

表B LQE01(编号) LQE17(状态)
  100010001 0
  100010002 1
  100010003 1
  100010004 0
  . .
  . .
  . .
  100011190 1

我现在要在表B中找出存在于在表A中,并且LQE17状态等于0的编号,要怎么写啊!!!!

作者: xlh0053   发布时间: 2011-12-14

SQL code
select * from b where exists(select 1 from a where LQE01=b.LQE01) and LQE17=0

作者: fredrickhu   发布时间: 2011-12-14

是这个意思?
SQL code
select LQE01 from 表B b
where LQE17=0
and exists(select 1 from 表A where b.LQE01 between LPZ03 and LPZ04)

作者: qianjin036a   发布时间: 2011-12-14

SQL code
select b.* from 表B b,表A a  
where b.[LQE01] between  a.[LPZ03] and a.[LPZ04]
      and b.[LQE17]=0

作者: ssp2009   发布时间: 2011-12-14

SQL code
select * from b where exists(select 1 from a where LQE01 between LPZ03 and LPZ04) and LQE17=0

作者: fredrickhu   发布时间: 2011-12-14

select b.* from 表B b,表A a  
where b.[LQE01] between a.[LPZ03] and a.[LPZ04]
  and b.[LQE17]=0
对吗 ?

作者: szstephenzhou   发布时间: 2011-12-14

1楼和2楼的能解释一下么?

作者: xlh0053   发布时间: 2011-12-14

引用 6 楼 xlh0053 的回复:

1楼和2楼的能解释一下么?


SQL code
select LQE01 from 表B b  --到表B中找,设置一个别名(也可以不设)
where LQE17=0        --这是第一个条件
and exists(select 1 from 表A where b.LQE01 between LPZ03 and LPZ04)
--用子查询实现第二个条件,即你要找的记录必须在表A中找得到 LQE01>=表A.LPZ03 且 LQE01<=表A.LPZ04
--语句中用别名代替了表名,如果前面不设置别名的话,直接用表A就行了.

作者: qianjin036a   发布时间: 2011-12-14

1楼的帅锅强大

作者: xlh0053   发布时间: 2011-12-14

exists 的意思就是存在这样一个结果.
如果返回1 就是 true 了,返回NULL(即找不到),就是 false.

作者: qianjin036a   发布时间: 2011-12-14

好像不大对啊,那我能算出表A中券的数量吗?这样我才能核对一下

作者: xlh0053   发布时间: 2011-12-14

引用 8 楼 xlh0053 的回复:
1楼的帅锅强大


F姐表示有很大的压力

作者: jiemo587   发布时间: 2011-12-14

SQL code
create table 表A(LPZ03 varchar(10),LPZ04 varchar(10))
insert into 表A select '100010001','100010010'
insert into 表A select '100010099','100010150'
insert into 表A select '100011100','100011110'
insert into 表A select '100011150','100011190'
create table 表B(LQE01 varchar(10),LQE17 int)
insert into 表B select '100010001',0
insert into 表B select '100010002',1
insert into 表B select '100010003',1
insert into 表B select '100010004',0
insert into 表B select '100011190',1
go
select LQE01 from 表B b
where LQE17=0
and exists(select 1 from 表A where b.LQE01 between LPZ03 and LPZ04)
/*
LQE01
----------
100010001
100010004

(2 行受影响)

*/
go
drop table 表A,表B

作者: qianjin036a   发布时间: 2011-12-14