作者kolynn ()
站内Database
标题[SQL ] Union 与 Join 练习疑问?
时间Wed Nov 14 16:30:27 2012
小弟对SQL语法不甚了解,在练习 Union 单元时异想天开
想其他方式想达到同样的效果。
使用资料库:Northwind 使用表单:Orders,Customers
练习 Union 时下了这个语法:
select customerid ID,shipcity City
from orders
union
select customerid,city
from customers
出现了92笔资料。
接着实验以下两种方法:
1.
select distinct o.customerid ID,o.shipcity City
from orders o full join customers c
on o.customerid = c.customerid
select distinct c.customerid ID,city City
from customers c full join orders o
on c.customerid = o.customerid
order by c.customerid
2.
select customerid,city
from customers
where exists (select customerid,shipcity from orders)
order by customerid
各出现了91笔资料。
发现:
Union 的方法其中出现了两笔重复的资料。
---------------------
| ID | City |
---------------------
|AROUT |Colchester |
|AROUT |London |
---------------------
方法1.、2.则少於Union法一笔,
只会出现上表格其中一笔。
---------------------
| ID | City |
---------------------
|AROUT |Colchester |
---------------------
或
---------------------
| ID | City |
---------------------
|AROUT |London |
---------------------
结论:
其後查询过原始的表单,在 Orders 表单里有笔资料里的栏位ShipCity
与 Customers 表单设定的栏位 City 值不相同,导致做 join 与 exists
时只能出现某一个CustomerID栏位,不能出现两个不同 City 栏位。
疑问:
这种资料库的资料程现方式可能发生在实务的例子里吗?
有别的方法,可以查询出与 Union 方法一样的结果吗?
(不好意思小弟第一次发文,有任何奇怪的问题请鞭小力一点>_<
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 125.224.181.181
1F:推 LaPass:你把两种东西的用途错了啦..... 11/15 10:18