DQL单表查询

DQL
数据查询语言
数据查询关键字:select
对数据库关系表中的数据进行查询

创建数据库
创建表格
学生表(学号s_no,姓名s_name,班级s_classno,课程s_courseno)

班级表(班级编号c_no,班级名称c_name,班级地点c_d)

课程表(课程编号cou_no,课程名称cou_name,课程学分cou_score)


1.查询表中所有字段
select*from表名;

2、查询指定字段
select 属性名1,属性名2,…,属性名n from 表名;
条件查询

3、查询指定字段符合相关条件的记录
select 属性名1,属性名2,…,属性名n from 表名 where 表达式;
where子句后的表达式格式为:属性名 运算符 值。
where表达式中,可以有<,>,=,<>,!=,>=,<=,[not] between and,like(通配符%,_模糊匹配)
%匹配任意多个字符
_匹配任意一个字符

like "张%" 匹配:张三,张长弓,张无忌……
like "张_"匹配:张三,张二,张一

like "%张_" 匹配:一二三四张四
不能匹配小张 ;

4、多条匹配查询
where 表达式1 and 表达式2;
where 表达式1 or 表达式2; or可以用||来代替
select * from student where s_no=1 or s_name='张三';

5、在集合中查询
in
select *from student where s_no in(1,6,3);


6、在集合外查询
not in
select *from student where s_no not in(2,4);

7、查询空值
is null
select * from class where s_birchday is null;

8、查询非空值
关键字:is not null(不是not is null)
select * from class where s_birchday is not null;

9、查询去重数据
distinct
select distinct s_classno from student;
注意:distinct只能对一个属性去重


10、分页查询
limit [n,]m
n是偏移量,m只元组的条数
从1+n条数据开始,向后读取每条数据

11、合并查询结果集
union或union all
union 联合同时去重
union all 联合所有

结果集和临时表:select的数据不是直接从表中显示出来的,而是显示在结果集中 是虚拟表 我们看起来一样

12、复制表数据
insert into 表1 select * from 表2

13、通过查询给已存在的表插入值
insert into stu1 select s_no,s_name from student;

14、select into
从一个表选取数据(全部或者部分),然后把数据插入到另一个新建表中。
select *into newstudent from student;

15、通过查询给新建表赋值
create table stu2(select*from student);
只复制内容,主键需要另外复制

16、取别名
as(as可以省略 用空格替代)

17、排序
order by 属性名(升序)
order by 属性名desc(升序)

猜你喜欢

转载自www.cnblogs.com/liugangjiayou/p/11710466.html