update employee set salary = 5000; //将对表中所有的记录进行修改
update employee set salary = 3000 where name = '张飞'; update employee set salary = 4000 ,job='高级打手' where name = '关羽'; update employee set salary = salary+1000 where name='刘备';DELETE delete from employee where name = '张飞'; delete from employee; //表还在,只是所有记录被清空了 truncate table employee; //表被删掉,再重新建了一张空表SELECT create table exam ( id int primary key auto_increment, name varchar(20) not null, chinese double, math double, english double );insert into exam values(null, '关羽', 85,76,70);
insert into exam values(null, '张飞', 55,36,50); insert into exam values(null, '赵云', 81,56,73); 1.基本的查询 select * from exam; select name,english from exam; //查询指定的列 select distinct english from exam; //查询结果去除重复的行 select name, math+10, english+10,chinese+10 from exam; //所有科目加10分,数据库数据没有变 select name, english+math+chinese from exam;//查询所有学生的总分 select name as 姓名, english+math+chinese as 总成绩 from exam; //查询所有学生的总分,通过as起别名显示列,省略掉as也可以 select name english from exam; //把english作为name的别名 2.使用where语句过滤查询 比较运算符 > < <= >= = <> :大于、小于、大于(小于)等于、不等于 between ...and... : 显示在某一区间的值 in(set) : 显示在in列表中的值,例:in(100,200) like ‘张pattern’ : 模糊查询%_ Is null : 判断是否为空 逻辑运算符 and or not select * from exam where name='张飞'; select * from exam where english > 90; //分数大于90的 select name 姓名, math+english+chinese 总分 from exam where math+english+chinese > 200; //查询总分大于200的同学 select * from exam where english between 80 and 100; select * from exam where english in(75,76,70); select * from exam where name like '张%'; //查姓张的人 select * from exam where name like '张_'; //查姓张的人,姓名只有2个字 select * from exam where math > 70 and chinese > 80; 3.使用order by 关键字对查询结果进行排序操作 asc:升序 desc:降序 select name,math from exam order by math; //默认升序 select name,math from exam order by math desc; select name 姓名, chinese + math + english 总成绩 from exam order by 总成绩 desc; //总成绩降序排列 select name 姓名, chinese + math + english 总成绩 from exam where name like '张%' order by 总成绩 desc ; 4.使用聚集函数 count: 用来统计符合条件的行的个数 select count(*) from exam //统计有多少个学生 select count(*) from exam where math > 90; //数学大于90的个数 select count(*) from exam where chinese + math + english > 200;//总分>200的个数 sum: 用来将符合条件的记录的指定列进行求和 select sum(math) from exam; //数学成绩和 select sum(math),sum(english),sum(chinese) from exam; select sum(math+english+chinese) from exam; 只要某一行有NULL存在,那么整行都是NULL select sum(ifnull(math, 0) + ifnull(english, 0) + ifnull(chinese, 0)) from exam; select sum(chinese)/count(*) 语文平均分 from exam; //求语文平均分 avg: 用来计算符合条件记录的指定列的平均值 select avg(chinese) from exam; //语文平均分 select avg(ifnull(math, 0) + ifnull(english, 0) + ifnull(chinese, 0)) from exam; //总分平均分 max/min: 返回满足where条件的一列的最大值/最小值 select max(ifnull(math, 0) + ifnull(english, 0) + ifnull(chinese, 0)) from exam; select min(ifnull(math, 0) + ifnull(english, 0) + ifnull(chinese, 0)) from exam; 5.分组查询 create table orders( id int, product varchar(20), price float ); insert into orders(id, product, price) values(1,'电视',900); insert into orders(id, product, price) values(2,'洗衣机',100); insert into orders(id, product, price) values(3,'洗衣粉',90); insert into orders(id, product, price) values(4,'桔子',9); insert into orders(id, product, price) values(5,'洗衣粉',90);group by 分组显示
select * from orders group by product; //分组 select product, sum(price) from orders group by product; //sum是对摞在一起的记录聚合 having :对分组后的结果进行过滤显示 where 和 having 的区别: where在分组之前过滤,having在分组之后过滤 having中可以使用聚合函数,where不能使用 很多请客下使用where子句的地方可以使用having 查询购买了几类商品,并且每类总价大于100的商品 select product 商品名称, sum(price) 总价 from orders group by product having sum(price) > 100; 查询单价小于100而总价大于150的商品名称 select product 商品名称, price 单价, sum(price) 总价 from orders group by product having price < 100 and sum(price) > 150;sql语句的书写顺序: select from where group by having order bysql语句的执行顺序 from where select group by having order by