【www.gdgbn.com--php高级应用】

sql 高级查询运算词 union except right及外部连接
a: union 运算符
union 运算符通过组合其他两个结果表(例如 table1 和 table2)并消去表中任何重复行而派生出一个结果表。当 all 随 union 一起使用时(即 union all),不消除重复行。两种情况下,派生表的每一行不是来自 table1 就是来自 table2。

sql union 语法
select column_name(s) from table_name1
union
select column_name(s) from table_name2

看个简单实例

select e_name from employees_china
union all
select e_name from employees_usa


b: except 运算符
except 运算符通过包括所有在 table1 中但不在 table2 中的行并消除所有重复行而派生出一个结果表。当 all 随 except 一起使用时 (except all),不消除重复行。

select * from testx except select * from testy
在testx的数据但在 testy中没有重复的

注:
sql server 2000中不能使用except(只能使用union这个集合操作关键字),貌似2005可以

c: intersect 运算符
intersect 的语法如下:

[sql语句 1]
intersect
[sql语句 2]

 

intersect 运算符通过只包括 table1 和 table2 中都有的行并消除所有重复行而派生出一个结果表。当 all 随 intersect 一起使用时 (intersect all),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。

select date from store_information
intersect
select date from internet_sales


 
12、说明:使用外连接
a、left (outer) join:
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
sql: select a.a, a.b, a.c, b.c, b.d, b.f from a left out join b on a.a = b.c
b:right (outer) join:
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
c:full/cross (outer) join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
12、分组:group by:
  一张表,一旦分组 完成后,查询后只能得到组相关的信息。
 组相关的信息:(统计信息) count,sum,max,min,avg  分组的标准)
    在sqlserver中分组时:不能以text,ntext,image类型的字段作为分组依据
 在selecte统计函数中的字段,不能和普通的字段放在一起;
13、对数据库教程进行操作:
 分离数据库: sp_detach_db; 附加数据库:sp_attach_db 后接表明,附加需要完整的路径名
14.如何修改数据库的名称:
sp_renamedb "old_name", "new_name"

本文来源:http://www.gdgbn.com/jiaocheng/28209/