select top 子句用于规定要返回的记录的数目。
例如:
select top 50 percent * from Score; //从 Score表中选取前面百分之 50 的记录
select top 5 * from Score; //获取前5行记录
select top 5 * from Score order by desc; //获取后5行记录
--desc 表示降序排列 asc 表示升序
like 操作符
select * from student where name like '李华'; //从学生表中获取李华的全部记录
select * from student where name not like '李华'; //从学生表中获取除李华外的全部记录
'%a' //以a结尾的数据
'a%' //以a开头的数据
'%a%' //含有a的数据
‘_a_’ //三位且中间字母是a的
'_a' //两位且结尾字母是a的
'a_' //两位且开头字母是a的
'%' //替代一个或多个字符
'_' // 仅替代一个字符
[charlist] 字符列中的任何单一字符
[^charlist] 或者 [!charlist] 不在字符列中的任何单一字符
其中搭配以上通配符可以让LIKE命令实现多种技巧:
1、like 'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。
2、like '%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。
3、like '%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。
7、LIKE'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。
操作符 in & not in :允许在 WHERE 子句中规定多个值。
语法: select * from student where name in ('阿珍','阿强'); //获取student表中名为阿珍和阿强的所有信息
in 与 = 的异同
- 相同点:均在where中使用作为筛选条件之一、均是等于的含义
- 不同点:'in' 可以规定多个值,'=' 规定一个值
in 与 = 的转换
in 的语法:select * from student where name in ('阿珍','阿强');
= 的语法:select * from student where name = '阿珍' or name = '阿强';
操作符 between & not between :选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期
select * from student where birthday between '2018-09-01' and '2022-07-01'; //获取生日为2018-09-01到2022-07-01的学生的信息
别名 as
列的别名语法:select st_id as 学号 from student;
表的别名语法:select st_id from student as 学生表;
连接(join)
- inner join:如果表中有至少一个匹配,则返回行,取交集。
- 语法:select * from student inner join Score on st_id = st_id;
- 注释:inner join 与 join 是相同的。
- left join:即使右表中没有匹配,也从左表返回所有的行,取 1全部,2 没有对应的值为 null。
- 语法:select * from student left join Score on st_id = st_id;
- right join:即使左表中没有匹配,也从右表返回所有的行,取2全部,1没有对应的值为 null。
- 语法:select * from student rigth join Score on st_id = st_id;
full join:只要其中一个表中存在匹配,则返回行,取并集,彼此没有对应的值为 null。
语法:select * from student full outer join Score on st_id = st_id;
操作符 union
select country from websites union select country from apps; //连接两个表的查询结果集,重复的不显示 select country from websites union all select country from apps order by country; /连接俩个个表的查询结果集,显示重复 select country,name from websites where country = 'CN' union all select country,app_name from apps where country='CN' order by name; //通过where条件查询的结果,连接连个表的结果集,并根据名字排序。
select into语句:从一个表复制数据,然后把数据插入到另一个新表中。
语法:select * into newtable from table; //复制table 的数据插入到newtable
insert into select语句:从一个表复制数据,然后把数据插入到一个已存在的表中。
语法:insert into table2 select * from table1; //从table1表中复制所有的列插入到已存在的table2表中
select into from 和 insert into select 都是用来复制表
两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建;insert into select from 要求目标表存在。
创建数据库的表create table
语法:
create table table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size)
);
注:
column_name 参数规定表中列的名称。
data_type 参数规定列的数据类型(例如 varchar、integer、decimal、date 等等)。
size 参数规定表中列的最大长度。
约束的知识点见下一篇的SQL常用语句(进阶2.0)-----学习笔记