1、select * from customer where
exists(select * from orderswhere orders.客户编号=customer.客户编号)
与
select * from customer where exists
(select * from orders,customer where orders.客户编号=customer.客户编号)
有什末区别?
2、select a.* from orders a join orders b
on a.客户编号=b.客户编号 and a.订单号<>b.订单号
帮忙解释一下这条语句的意思。
exists(select * from orderswhere orders.客户编号=customer.客户编号)
与
select * from customer where exists
(select * from orders,customer where orders.客户编号=customer.客户编号)
有什末区别?
2、select a.* from orders a join orders b
on a.客户编号=b.客户编号 and a.订单号<>b.订单号
帮忙解释一下这条语句的意思。
insert into orders values(1,1)
insert into orders values(1,2)
insert into orders values(1,3)
insert into orders values(2,1)
insert into orders values(2,2)
create table customer(客户编号 int,订单号 int)
insert into customer values(1,1)
insert into customer values(2,1)
go
select * from customer where exists (select * from orders where orders.客户编号 = customer.客户编号)
/*
客户编号 订单号
----------- -----------
1 1
2 1
*/
select * from customer where exists (select * from orders , customer where orders.客户编号 = customer.客户编号)
/*
客户编号 订单号
----------- -----------
1 1
2 1
*/
select a.* from orders a join orders b on a.客户编号 = b.客户编号 and a.订单号 <> b.订单号 order by a.客户编号,a.订单号
/*
客户编号 订单号
----------- -----------
1 1
1 1
1 2
1 2
1 3
1 3
2 1
2 2
*/drop table orders,customer
1、select * from customer where exists
(select * from orders where orders.客户编号=customer.客户编号)
--取customer表中客户编号存在于orders表中的数据。
select * from customer where exists
(select * from orders,customer where orders.客户编号=customer.客户编号)
--与上句没有区别。。
select a.*
from orders a inner join customer b
where a.客户编号=b.客户编号2. 应该只是比1多了一个条件 a.订单号 <> b.订单号
1、select * from customer where
exists(select * from orders where orders.客户编号=customer.客户编号)
2、select * from customer where exists
(select * from orders,customer where orders.客户编号=customer.客户编号)
exists(select * from orderswhere orders.客户编号=customer.客户编号)
对curtomer中的每条记录,查询 select * from orders 中客户编号=curtomer表中当前记录的客户编号,如果查询有结果,则返回customer表的记录
如果不存在,则不返回记录.
其中子查询与外面表customer有关连select * from customer where exists
(select * from orders,customer where orders.客户编号=customer.客户编号)
对curtomer中的每条记录, 检验查询select * from orders,customer where orders.客户编号=customer.客户编号是否存在,
如果存在,则返回记录,如果不存在则不返回,其中select * from orders,customer where orders.客户编号=customer.客户编号
是一独立的查询,并未与外面的customer表发生关连.本人描述能力一般,可能说得不是很清楚.
2、select a.* from orders a join orders b
on a.客户编号=b.客户编号 and a.订单号 <> b.订单号
帮忙解释一下这条语句的意思。
orders表的记录取出取别名 为a,再取出orders表的记录取别名为 B
a与b 内连接,取出其中 两记录集中客户编号相同且订单号不同的记录
因为1.中第二个查询中的子查询是一独立的查询(说它独立,是因为将子查询单独拿出,可独立运行),并不与外部的customer表发生关连,如果查询存在,则customer表中的记录全部出来,
如果查询不存在,则customer表中的记录全部不出来.
而第一个查询中,每扫描过一条记录,都将此记录的 客户编号取出作为与子查询的客户编号比较的where条件,根据子查询是否存在,决定记录是否返回.
正解!
第二个我问得有点问题。有空的话再帮我看看说说第二个问题吧。先谢了哈!以下是我orders表中的数据:
订单号 货品名称 客户编号 数量 总金额 订货日期
----------- -------------------- ----------- ----------- --------------------- ------------------------------------------------------
1 pen 2 10 156.0000 2003-03-24 00:00:00.000
2 pen 4 70 1092.0000 2003-03-24 00:00:00.000
3 desk 2 10 2000.0000 2003-03-24 00:00:00.000
4 desk 1 20 4000.0000 2003-03-24 00:00:00.000
5 book 2 5 NULL 2003-03-25 00:00:00.000
当我执行
select a.* from orders a join orders b
on a.客户编号=b.客户编号 and a.订单号 <> b.订单号
是出来的结果是:
订单号 货品名称 客户编号 数量 总金额 订货日期
----------- -------------------- ----------- ----------- --------------------- ------------------------------------------------------
3 desk 2 10 2000.0000 2003-03-24 00:00:00.000
5 book 2 5 NULL 2003-03-25 00:00:00.000
1 pen 2 10 156.0000 2003-03-24 00:00:00.000
5 book 2 5 NULL 2003-03-25 00:00:00.000
1 pen 2 10 156.0000 2003-03-24 00:00:00.000
3 desk 2 10 2000.0000 2003-03-24 00:00:00.000
我想问一下为什末结果显示出来了两次啊?
a:订单号 1 3 5
b:订单号 1 3 5
a的订单号 1 不等于 b的订单号 3 5
a的订单号 3 不等于 b的订单号 1 5
a的订单号 5 不等于 b的订单号 1 3
所以每個就出兩條阿 不想出重復紀錄的話 就用distinct
a:订单号 1 3 5
b:订单号 1 3 5
a的订单号 1 不等于 b的订单号 3 5
a的订单号 3 不等于 b的订单号 1 5
a的订单号 5 不等于 b的订单号 1 3
所以每個就出兩條阿 不想出重復紀錄的話 就用distinct
a:订单号 1 3 5
b:订单号 1 3 5
a的订单号 1 不等于 b的订单号 3 5
a的订单号 3 不等于 b的订单号 1 5
a的订单号 5 不等于 b的订单号 1 3
所以每個就出兩條阿 不想出重復紀錄的話 就用distinct