部门表
create table TB_DEPT
(
ID NUMBER,
DEPTNAME VARCHAR2(20)
)
测试数据
1 部门1
2 部门2
3 部门3
员工表
create table tb_user (id number(10),username varchar2(20),age smallint ,depno number(10))
测试数据
1 张三 20 1
2 李四 23 2
3 王五 30 3SQL:
select * from tb_user where depno in(
select id from tb_dept where username='张三'
)
查询结果
1 张三 20 1问题:
这个sql 语句 是如何执行的,tb_dept 表中没有username 字段 如何能查出结果来 ,哪位大侠给解释一下。 SQLSELECT
create table TB_DEPT
(
ID NUMBER,
DEPTNAME VARCHAR2(20)
)
测试数据
1 部门1
2 部门2
3 部门3
员工表
create table tb_user (id number(10),username varchar2(20),age smallint ,depno number(10))
测试数据
1 张三 20 1
2 李四 23 2
3 王五 30 3SQL:
select * from tb_user where depno in(
select id from tb_dept where username='张三'
)
查询结果
1 张三 20 1问题:
这个sql 语句 是如何执行的,tb_dept 表中没有username 字段 如何能查出结果来 ,哪位大侠给解释一下。 SQLSELECT
然后再根据条件检索而且你的SQL语句检索的内容是tb_user表 不是tb_dept表
tb_dept的检索 只是作为 tb_user表检索的条件而已
select * from tb_user where depno in( id的所有值 )
select id from tb_dept where username='张三'
)
select * from tb_user where depno exists(select id from tb_dept where tb_user.username=tb.dept and username='张三');
select * from tb_user u where u.depno in(
select d.id from tb_dept d where u.username='张三'
)所以,上面的写相当于。
select * from tb_user u where u.depno in(
select d.id from tb_dept d
)
and u.username='张三'http://item.taobao.com/item.htm?spm=686.1000925.1000774.41.OGsWSi&id=20111213320