1.高级查询运算词
------ union运算符
union运算符通过组合其他两个结果表(例如table1和table2)并消去表中任何重复行而派生出一个结果表。当 all 随 union 一起使用时(即 union all),不消除重复行。两种情况下,派生表的每一行不是来自 table1就是来自 table2。
------ except运算符
except 运算符通过包括所有在 table1 中但不在 table2 中的行并消除所有重复行而派生出一个结果表。当all 随 except 一起使用时 (except all),不消除重复行。
------ intersect 运算符
intersect 运算符通过只包括 table1 和 table2 中都有的行并消除所有重复行而派生出一个结果表。当 all 随 intersect 一起使用时 (intersect all),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
2.使用连接
------左连接:left join
结果集几包括连接表的匹配行,也包括左连接表的所有行。
select a.a, a.b, a.c, b.c, b.d, b.f from a
left join b on a.a = b.c
------右连接:right join
结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
select a.a, a.b, a.c, b.c, b.d, b.f from a
right join b on a.a = b.c
------内连接:inner join
不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
select a.a, a.b, a.c, b.c, b.d, b.f from a
inner join b on a.a = b.c
3.子查询
select a,b,c from a where a.a in (select d from b )
或者:
select a,b,c from a where a.a in (1,2,3)
4.between
限制查询数据范围时包括了边界值,not between 不包括。
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值 1 and 数值 2
5.in
select * from table1 where a in ('值1','值2','值3')
或者:
select * from table1 where a not in ('值1','值2','值3')