查询所有字段
select * from 表名;
查询表中的sname字段
select sname from 表名;
select sname,sid from 表名;
查询学号为S_1001,或者姓名为liSi的记录
select * from stu where sid='S_1001' or sname='liSi';
查询学号为S_1001,S_1002,S_1003的记录
select * from stu where sid in ('S_1001','S_1002','S_1003');
查询学号不是S_1001,S_1002,S_1003的记录
select * from stu where sid not in ('S_1001','S_1002','S_1003');
查询年龄在20到40之间的学生记录
1. select * from stu where age between 20 and 40;(包括20和40)
2. select * from stu where age>=20 and age<=40;
查询性别非男的学生记录
1. select * from stu where gender !='male';
2. select * from stu where gender <>'male';
3.select * from stu where not gender ='male';
查询姓名不为null的学生记录
select * from stu where sname is not null;
select * from stu where not sname is null;
1关系运算符 = != > < >= <= <>(不等于)
2.between and 包括范围
3.is not null
is null
4.and 与
or 或
not 非
5.in() 在什么范围中查询
not in()
字段控制查询
去除重复数据
select distinct sal from emp;
查看每个人佣金和薪金的和
相当于查询了一个新的字段
注意如果一个值和null相加 结果为null
select empno ,sal+comm from emp;
可以在查询的时候给null赋值
select empno,sal+IFNULL(comm,0)from emp;
select * ,sal+IFNULL(comm,0)from emp;
查询时可以给字段起个名
使用as关键字 可以省略
select * ,sal+IFNULL(comm,0)as total from emp;
select * ,sal+IFNULL(comm,0) total from emp;
empno job起别名
select *,empno as n, job as j from emp;
按学生年龄排序
默认升序(asc) 降序 desc
注意sql语句的关键字不要与表名重复
select * from stu order by age desc;
查询所有雇员,按月薪降序排序,如果月薪相同时,按编号降序排序
select * from emp order by sal desc ,empno desc;
模糊查询
查询名字中带l的人的所有信息
l% l开头 %l l结尾 %表示很多字母
_l_ _表示单个字母 allen
select * from emp where ename like '%l%';
select * from emp where ename like '_ll__';
select * from emp where ename like '_l';
聚合函数(自动过滤空值)
count查询的是一共有多少条记录(行)
select count(*)from emp;
查询公司sal的和(查询时记录数要相同)
select count(*) num ,sum(sal) sum from emp;
查询emp表中有佣金的人数
自动过滤空值
select count(comm) from emp;
查询表中月薪大于2500的人数
select count(*) from emp where sal>2500 ;
统计月薪与佣金之和大于2500元的人数:
select count(*) from emp where sal+IFNULL(comm,0)>2500;
查询有佣金的人数,有领导的人数:
select count(comm),count(mgr)from emp ;
查询所有雇员月薪和,以及所有雇员佣金和:
select sum(sal),sum(comm) from emp;
查询所有雇员月薪+佣金和:
select sum(sal+IFNULL(comm,0))from emp;
统计所有员工平均工资
select avg(sal+IFNULL(comm,0))from emp;
查询最高工资和最低工资
select max(sal),min(sal)from emp;
分组查询 group by
查询每个部门的部门编号和每个部门的工资和
如果是分组查询 group by 后面的字段才可以查询字段的位置
select deptno ,sum(sal) from emp group by deptno;
查询每个部门的部门编号以及每个部门的人数:
select deptno ,count(deptno) from emp group by deptno;
查询每个部门的部门编号以及每个部门工资大于1500的人数:
先where删选 再分组
select deptno ,count(deptno)from emp where sal>1500 group by deptno;
查询工资总和大于9000的部门编号以及工资和
where后不加聚合函数
having是分组以后使用的筛选关键字
having后可以跟聚合函数
select deptno,sum(sal) from emp GROUP BY deptno HAVING sum(sal)>9000 ;
分页查询limit关键字
查询前三条数据
参数1 从哪条开始
参数2 一共查几条
select * from emp limit 0,3;
数据完整性
实体完整性
主键约束
创建表stu1 sid sname sid为主键
主键特点唯一且不能为null
方式1
create table stu1(
sid int primary key,
name varchar(20)
);
insert into stu1 values(1,'a'),(2,'s'),(1,'d');
-- 方式2 可以用来创建联合主键
create table stu2(
sid int ,
name varchar(20),
primary key(sid)
);
可以用来创建联合主键
当两个字段的值完全一样才重复
create table stu3(
classid int,
sid int ,
name varchar(20),
primary key(sid,classid)
);
方式3
create table stu4(
sid int ,
name varchar(20)
);
添加主键约束
alter table stu4 add constraint primary key(sid);
删除主键约束
alter table stu4 drop primary key;
-- 唯一约束
-- 值唯一 (可以为空)
create table stu5(
sid int primary key,
name varchar(20) unique
);
-- 自动增长
-- 自动加1 出现过的就不出现了
create table stu6(
sid int primary key auto_increment,
name varchar(20) unique
);
insert into stu6 (name) values('a');
insert into stu6 (name,sid) values('d',null);
域完整性
限制单元格内的数据完整性
create table stu8(
sid int primary key auto_increment,
sname varchar(20) not null ,
sgender varchar(20) default'男'
);
引用语约束(参照物约束)
主表和从表有依赖关系 从表依赖主表 这时可以给从表添加一个约束 外键约束
create table student1(
sid int primary key,
sname varchar(20)
);
create table score1(
sid int ,
score int ,
constraint fk_stu_score_sid foreign key(sid)
references student1(sid)
);