sql 高级查询运算词 UNION EXCEPT right及外部连接

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'

时间: 2024-09-19 06:29:26

sql 高级查询运算词 UNION EXCEPT right及外部连接的相关文章

sql高级查询语句 麻烦大家了

问题描述 sql高级查询语句 麻烦大家了 --28)查询选修了课程名为"数据库"的学生的学号和姓名. select sname,sno from course,student where course.cno in( select cno from course where cname='sql') and course.cname=student.sno group by sname having count(cname)='sql' 显示错误 出不来 解决方案 sql高级查询语句

(转载)SQL高级查询技巧

1.UNION,EXCEPT,INTERSECT运算符 A,UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表. 当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行.两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2. B, EXCEPT 运算符 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表

SQL Server SQL高级查询语句小结_MsSql

Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select distinct sex from student; --count 统计 select count(*) from student; select count(sex) from student; select count(distinct sex) from student; --top

SQL Server SQL高级查询语句小结

Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select distinct sex from student; --count 统计 select count(*) from student; select count(sex) from student; select count(distinct sex) from student; --top

sql语句查询结果合并union all用法

整理别人的sql 大概的思想是用union 和union all --合并重复行select * from Aunion select * from B --不合并重复行select * from Aunion allselect * from B 按某个字段排序--合并重复行select *from (select * from Aunion select * from B) AS Torder by 字段名 --不合并重复行select *from (select * from Aunion

SQL高级查询技巧(两次JOIN同一个表,自包含JOIN,不等JOIN)

掌握了这些,就比较高级啦 Using the Same Table Twice 如下面查询中的branch字段 SELECT a.account_id, e.emp_id, b_a.name open_branch, b_e.name emp_branch FROM account AS a INNER JOIN branch AS b_a ON a.open_branch_id = b_a.branch_id INNER JOIN employee AS e ON a.open_emp_id

SQL高级查询

  t_tudent(sid,sname,sage,ssex,sdept) 学生表 t_course(cid,cname,tid) 课程表 t_score( scid,sid,cid,grade) 成绩表 t_teacher(tid,tname) 教师表 问题: 1.查询"001"课程比"002"课程成绩高的所有学生的学号; select t1.sid from (select sid,grade from t_score where cid = '001') t1

SQL Server调优系列基础篇(子查询运算总结)

原文:SQL Server调优系列基础篇(子查询运算总结) 前言 前面我们的几篇文章介绍了一系列关于运算符的介绍,以及各个运算符的优化方式和技巧.其中涵盖:查看执行计划的方式.几种数据集常用的连接方式.联合运算符方式.并行运算符等一系列的我们常见的运算符.有兴趣的童鞋可以点击查看. 本篇我们介绍关于子查询语句的一系列内容,子查询一般是我们形成复杂查询的一些基础性操作,所以关于子查询的应用方式就非常重要. 废话少说,开始本篇的正题. 技术准备 数据库版本为SQL Server2008R2,利用微软

Sql Server 存储过程中查询数据无法使用 Union(All)

原文:Sql Server 存储过程中查询数据无法使用 Union(All) 微软Sql Server数据库中,书写存储过程时,关于查询数据,无法使用Union(All)关联多个查询.     1.先看一段正常的SQL语句,使用了Union(All)查询:      SELECT ci.CustId --客户编号 , ci.CustNam --客户名称 , ci.ContactBy --联系人 , ci.Conacts --联系电话 , ci.Addr -- 联系地址 , ci.Notes --