数据查询是我们使用数据库使用得最多的功能,也是数据库核心操作之一。
1.一般格式
select [all | distinct]<目标列表达式>…
from <表名或者视图名>…
[where <条件表达式>]
[group by <列名1> [having <条件表达式>]] ------分组查询(having为分组条件)
[order by <列名 2> [ASC | desc]]; ------升序或者降序排列
2 单表查询
先创建一个学生表:
create table student(sno char(12) not null primary key,
sname char(12) not null,
sage samllint,
sex char(4) check(sex in("男","女")));
2.1 查询若干列
(1)查询全部列
例1.1 查询全体学生信息
select * from student;
(2)查询指定列
例1.2查询全体学生的学号,姓名
select sno,sname from syudent;
(3)查询列计算的值
例 1.3查询全体学生的姓名和出生年份
select sname,2019-sage from student;
2.2 选择表中的若干元组(也就是若干行,若干条数据)
(1)取消重复的行 使用distinct
例2.1查询所有不同年龄的学生信息
select distinct sage from student;
(2)查询满足条件的元组