DML:数据库操作语句 (增、删、改) 操作的是表中的记录
1.添加记录
INSERT INTO 表名 (字段名,...) VALUES (字段对应的值,...);
insert into student(id,age,name) values (1,18,'张三');
2.更新记录
UPDATE 表名 SET 字段名=新值,字段名=新值,... WHERE 更新条件;
update student set age=25 where id=1;
3.删除记录
DELETE FROM 表名 WHERE 删除条件;
delete from student where id=1;
当更新和删除不带条件时,更新或者删除所有记录
insert into student values (1,'aa',18),(2,'bb',19),(3,'cc',17);
insert into student (id,name) values (4,'oo');
DQL:查询
1.查询语句
SELECT 列1,列2,... FROM 表名 WHERE 查询条件;
SELECT *....; *就是所有列
select name from student;
2.为列起别名
select name as 姓名,age as 年龄 from student;
3.模糊查询 LIKE
%代表任意个字符
_代表任意一个字符
select * from student where name like '%龙%';
4.排序 ORDER BY
select * from student order by age [asc]; 按照年龄升序排列
select * from student order by age desc; 按照年龄降序排列
select * from student order by 字段1,字段2,...; 先按照字段1排序,如果字段1相等
才会使用字段2排序....
5.分页查询 LIMIT
select * from student limit 10; 获取前10条记录
第一个数字起始的行号,从0开始;第二个数字 要获取的记录数
select * from student limit 0,20; 获取前10条记录 1页
select * from student limit 20,20; 获取前10条记录 2页
order by和limit连用,order by必须在前面.
聚合函数:统计使用 如果某条记录的对应的要统计的字段值为NULL,不参与统计
count(字段名): 获取该字段的记录数
avg(字段名):该列的平均值
max(字段名):该列的最大值
min(字段名):该列的最小值
sum(字段名):该列和
分组查询 GROUP BY
select 聚合函数,分组条件的字段名 from 表名 group by 字段名
select sex,sum(sex) from student group by sex having sum(sex) > 100;
where 是在查询过程中筛选
having 是在查询结束后,在查询的结果上进行筛选
子查询:查询语句中嵌套了查询
select * from student where age > (select age from student where name='aa');
结果集联合 UNION:会不同结果集中的重复的记录,UNION ALL 不会去掉重复的