查询语言 select 的用法:
一,条件查询
-- 查询性别为女,并且年龄小于50的记录
select *from stu where gender='female' and age<50;
-- 查询学号为1001,或者姓名为liSi的记录
SELECT * FROM stu WHERE sid=1001 or sname='lisi';
-- 查询学号为1001,1002,1003的记录
select * from stu where sid=1001 or sid=1002 or sid=1003;
-- OR
SELECT * FROM stu WHERE sid in(1001,1002,1003);
-- 查询sid=1001-1007 的所有数据
select * from stu where sid BETWEEN 1001 and 1007 ;
-- 取别名
select sid 学生,sname 姓名,age 年龄,gender 性别 from stu;
-- 查询学号不是S_1001,S_1002,S_1003的记录
SELECT * FROM stu WHERE sid NOT in (1001,1002,1003);
-- 查询年龄为null的记录
select * from stu where age is null;
-- 查询年龄在20到405之间的学生记录
select * from stu where age between 20 and 45;
-- OR
select * from stu where age>=20 and age<=45;
-- 查询性别非男的学生记录
select * from stu where gender !='male';
SELECT * FROM stu WHERE gender <> 'male';
select * from stu where not gender='male';
-- 查询姓名不为null的学生记录
select * from stu where sname is not null;
select * from stu where sname !='null';
-- 查询age不为null的学生记录
select * from stu where not age is null;
select * from stu where age is not null;
模糊查询
关键字LIKE
%:任意个任意字符 _一个任意字符;
-- 查询姓名由5个字母构成的学生记录
select * from stu where sname like '_____';
-- 查询姓名由5个字母构成,并且第5个字母为“r”的学生记录
select * from stu where sname like '____r';
-- 查询姓名以“d”开头的学生记录
select * from stu where sname like 'd%';
-- 查询姓名中第2个字母为“i”的学生记录
select * from stu where sname like '_i%';
-- 查询姓名中包含“a”字母的学生记录
select * from stu where sname like '%a%';
insert into stu values(1012,'jiang%',67,'male');
-- 查询姓名中包含“%”字母的学生记录 特殊符号
select * from stu where sname like '%/%%' escape '/';
聚合函数:
关键字:count(数量),max(最大值),min(最小值),sum(求和),AVG(平均值)
select job from emp;
-- 求和
select sum(sal) from emp;
-- 求平均数
select avg(sal) from emp;
-- 最大值
select max(sal) from emp;
-- 最小值
select min(sal) from emp;
-- 有多少记录
select count(sal) from emp;
排序查询:
关键字:order by
-- 排序 顺序 asc 逆序 DESC 默认顺序
select * from emp ORDER BY sal;
select * from emp order by sal desc;
-- sal 降序 先sal降序 ,然后按照 empno 降序
select * from emp order by sal desc,empno desc;
* 分组查询*
关键字:group by
-- 根据部门分组
-- 查询部门编号及各部门人数
select deptno,count(*) from emp group by deptno;
-- 查询每个部门的部门编号和每个部门的工资和
select deptno,sum(sal) from emp group by deptno;
-- 查询每个部门的部门编号以及每个部门的人数
select deptno,count(*) from emp group by deptno;
-- 查询每个部门的部门编号以及每个部门工资大于1500的人数
select deptno,count(*) from emp where sal>1500 group by deptno;
-- 查询工资总和大于9000的部门编号以及工资和
select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;
-- 查询人数超过5个人的部门编号 和人数
select deptno,count(*) from emp group by deptno having count(*)>5;
-- 查询每个部门中 有comm的 各有多少人 deptno
select deptno,count(*) from emp where comm is not null group by deptno;
-- stu 男 女各有多少人
select gender,count(*) from stu group by gender;
分页查询
关键字:limit 0,5 表示起始位置 线是多少航
-- 第一页,前五行;
select * from emp LIMIT 0,5;
-- 第二页 再五行;
SELECT * FROM emp LIMIT 5,5;
-- 第三页 再五行
select * from emp limit 10,5;
主键
关键字:primary key 属性:不能为空(null),不重复
创建时加上主键
create table 表名 values(sid int primary key auto_increment,
sname varchar(20)
);
-- 删除主键
alter table stu drop primary key;
-- 加主键
alter table stu add primary key(sid);
-- 自增长
alter table stu change sid sid int auto_increment;
-- 修改时删除主键自增长
alter table stu change sid sid int;
* 非空 *
指定非空约束:
关键字:not null
CREATE TABLE stu(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(10) NOT NULL,
age INT,
gender VARCHAR(10)
);
* 唯一 *
关键字:unique 即‘不重复’
CREATE TABLE tab_ab(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(10) UNIQUE
);