条件查询
SELECT * FROM stu WHERE gender = 'female' AND age < 50;
SELECT * FROM stu WHERE sid = 'S_1001' OR sname = 'lisi';
SELECT * FROM stu WHERE sid='S_1001' OR sid='S_1002' OR sid='S_1003';
SELECT * FROM stu WHERE sid IN('S_1001','S_1002','S_1003');
select * from stu where age is null;
select * from stu where age between 20 and 40;
模糊查询
#模糊查询,需要使用关键字like,配合两个通配符来使用,_:表示任意一个字符;%表示任意0~n个字符
select * from stu where sname like '_____';
#五个字母
select * from stu where sname like '____i';
#五个字母,并且i结尾
select * from stu where sname like 'z%';
#z开头
select * from stu where sname like '_i%';
#第二位为i的信息
select * from stu where sname like '%a%';
#包含a的信息
字段控制查询
select deptno from emp;
select distinct deptno from emp;
#去除重复记录:distinct
select *,sal+comm from emp;
#求和
#null和任何数据运算,结果都为null
select *,sal+ifnull(comm,0) from emp;
#使用ifnull(字段名,数据)函数将某一个字段值为null的值转换成指定的数据
select empno as '员工编号',ename as '员工姓名' from emp;
#给字段取别名 as可以省略
select sal+ifnull(comm,0) '薪水' from emp;
排序
select * from emp order by sal;
#排序
select * from emp order by sal DESC;
#order by排序 ASC升序(默认,可以不写) DESC降序
select * from emp order by sal asc,empno desc;
聚合函数
select comm from emp;
select count(comm) as '有佣金的人数' from emp;
#聚合函数:用来对查询结果进行纵向统计的
select count(*) as '公司员工人数' from emp;
#count():统计指定列不为null的记录行数
select count(*) as '月薪大于2500' from emp where sal>2500;
select count(*) as '总和大于2500' from emp where sal+ifnull(comm,0)>2500;
select count(comm) as '有佣金人数',count(mgr) as '有领导人数' from emp;
select sum(sal) as 'all' from emp;
#求和sum()
select sum(sal) as 'all',sum(comm) as 'all2' from emp;
select sum(sal+ifnull(comm,0)) as 'all' from emp;
select avg(sal) from emp;
#求平均值avg()
select max(sal) as '最高工资',min(sal) as '最低工资' from emp;
#求最大max() 求最小min()
分组查询
select deptno,sum(sal) from emp group by deptno;
#根据部门编号,求每个部门工资和
select deptno,count(*) from emp group by deptno;
#根据部门编号,求每个部门人数
select deptno,count(*) from emp where sal>1500 group by deptno;
select gender,count(*) from stu group by gender;
select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;
#having是在分组后对数据进行过滤
LIMIT
select * from emp;
select * from emp limit 0,3;
select * from emp limit 2,3;
#从第几条开始,显示几条
实体完整性
主键约束
`id` int primary key
#主键具有唯一性,不能为null
`id` int
`name` varchar(50)
primary key(`id`,`name`)
`id` int
alter table student3 add primary key(`id`);
唯一约束
`name` varchar(50) unique
#unique修饰的值有唯一性
自动增长列
auto_increment
域完整性
数据类型:(数值类型、日期类型、字符串类型)
非空约束:not null
默认值约束 default
引用完整性(参照完整性)
#添加外键
create table if not exists `student5`(
`id` int primary key,
`name` varchar(50) not null,
`sex` varchar(50) default'男'
);
create table if not exists `score1`(
`id` int,-- 外键列的数据类型一定要与主键的类型一致
`score` int,
`sid` int,
constraint fk_score1_id foreign key(id) references student5(id)
);
#添加外键的第二种方式:两个表创建在创键的时候没有关联外键,可以通过修改表来创建外键
#将表score2里的sid字段引用外键,关联student6表中的sid字段
create table if not exists `student6`(
`sid` int primary key,
`name` varchar(50) not null,
`sex` varchar(10) default '男'
);
create table if not exists `score2`(
`id` int,
`score` int,
`sid` int -- 外键列的数据类型一定要与主键的类型一致
);
alter table score2 add constraint fk_student6_score2 foreign key(sid) references student6(sid);
多表查询
#合并结果集
select * from t1 union select * from t2;
select * from t1 union all select * from t2;
select * from t1,t2;
select * from emp,dept;
select * from emp,dept where emp.deptno = dept.deptno;
#进行过滤
select empno,ename,dept.deptno,dname from emp,dept where emp.deptno = dept.deptno;
select e.empno,e.ename,e.deptno,d.dname,d.loc from emp e,dept d where e.deptno = d.deptno;
#给表取别名:当表的名称比较长的时候,书写起来比较费事,这时候你可以给表取一个简单的别名
#上述sql语句就是内连接查询,不是标准的内连接查询,是mysql中的内连接查询
#标准内连接查询
#内连接查询的特点:不满足条件的数据不会显示出来,比如emp表中有一个员工zhangsan是50号部门,但是部门表中没有50号部门,所以zhangsan这条数据不会显示出来
select * from emp e inner join dept d on e.`deptno` = d.`deptno`;
select e.`empno`,e.`ename`,e.`deptno`,d.`dname` from emp e inner join dept d on e.`deptno` = d.`deptno`;
#外连接:外连接可以解决上述内连接中查询不出不满足条件的数据
#外连接分为左外连接和右外连接
select * from emp left outer join dept on emp.`deptno`=dept.`deptno`;
#左外连接:先将左边表emp的数据全部查询出来,然后再去右边表dept中查询数据,右边表满足条件的数据全部显示出来,不满足条件的数据所有字段全部显示为null
select * from emp right outer join dept on emp.`deptno`=dept.`deptno`;
#右外连接:先将右边表dept表中的数据全部查询出来,然后再去左边表emp中查询数据,左边表满足条件的数据全部显示出来,不满足条件的数据所有字段全部显示为null
#自然连接:不需要我们主动给出两个表中的过滤条件,而是通过mysql自动给出,要求进行连接查询的两个表中有相同名称和类型的字段
select * from emp natural join dept;
#自然内连接
select * from emp natural left join dept;
#自然左外连接
select * from emp natural right join dept;
#自然右外连接
#子查询:一个select语句中包含另一个select语句(select语句嵌套查询)
select * from emp where sal>(select sal from emp where ename='JONES');
select * from emp where deptno=(select deptno from emp where ename='SCOTT');
select * from emp where sal>(select MAX(sal) from emp where deptno=30);
select * from emp where job=(select job from emp where ename='MARTIN') and sal=(select sal from emp where ename='MARTIN');
select * from emp where(job,sal) in (select job,sal from emp where ename='MARTIN');
select * from emp where empno in(select mgr from emp group by mgr having count(mgr)>2);
select mgr from emp group by mgr having count(mgr)>2;