SQL语句
两个列的交集 INTERSECT
差集 EXCEPT
两个列的并集(去重) union
两个列的并集(不去重) union All
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行
增加一个列
Alter table tabname add column col type
列增加后将不能删除
取出表A中第31到第40记录
SQL语句为:
select * from A where ID >=31 and ID <= 40
ORACLE语句为:
select * from A where RowNum>=31 and RowNum<=40
ORACLE和SQL都能使用的是
select top 10 * from (select top 40 * from A order by ID) order by ID desc
或者是
select top 10 * from A where ID not in (select ID from top 30 from A)
select * from student
STUDENT_ID CLASS_ID
1 1
2 null
CLASS_ID CLASS_NAME
1 b
一、交叉连接(cross join)
交叉连接(cross join):有两种,显式的和隐式的,不带on子句,返回的是两表的乘积,也叫笛卡尔积。
隐式的交叉连接,没有cross join。
select o.*, c.* from student o , CLASS c
显式的交叉连接,使用cross join。
select o.* ,c.* from student o cross join CLASS c
STUDENT_ID CLASS_ID CLASS_ID CLASS_NAME
1 1 1 b
2 1 b
内连接 返回符合条件的值
select o.* ,c.* from CLASS c, student o where c.class_id=o.class_id;
外连接不但返回符合连接和查询条件的数据行,还返回不符合条件的一些行
左外连接还返回左表中不符合连接条件单符合查询条件的数据行。
右外连接还返回右表中不符合连接条件单符合查询条件的数据行。
全外连接还返回左表中不符合连接条件单符合查询条件的数据行,并且还返回右表中不符合连接条件单符合查询
左外连接
select o.* ,c.* from student o left join CLASS c on o.class_id = c.class_id
STUDENT_ID CLASS_ID CLASS_ID CLASS_NAME
1 1 1 b
2
可以在外链接后面再加where条件相当于过滤中间表
select o.* ,c.* from student o left join CLASS c on o.class_id = c.class_id where c.class_name is not null
union(合并集) 和 oracle minus(差集 ) db2 except (差集)
union all 是合并所有的 行 不删除相同的行
InterSect 交集 返回左边结果集和右边结果集中都有的记录。