# 使用的navicat 编辑的存储过程 CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_student_back`()BEGIN#定义max变量 DECLARE max INT DEFAULT 0; select max(id) into max from student_back; #判断是不是空值 是空值就赋值为0 if max is null then set max = 0; end if; #备份数据 INSERT INTO
数据库记录如下: 现在要查询统计出每个'name'的'result'分别为'success'和'fail'的次数: 利用IF条件判断满足条件为1,不满足为0,再用SUM函数求和,最后通过'name'分组. SELECT NAME, , )) AS success, , )) AS fail FROM if_test GROUP BY `name` 结果如下:
以下两条语句是否可以合并成一条: where b>'2015-10-12'; , e='2015-01-01' where b='2015-10-12'; 既然来写博客了,那答案肯定是可以的, 如下写法可以就上面的两条update语句合并成一条: , e=(case when b='2015-10-12' then '2015-01-01' else e end) where b>='2015-10-12'; 即: 当b等于'2015-10-12'时,e值设置为'2015-01-01' 当b大
显示每个地区的总人口数和总面积.仅显示那些面积超过1000000的地区. SELECT region, SUM(population), SUM(area) FROM bbc GROUP BY region HAVING SUM(area)> 在这里,我们不能用where来筛选超过1000000的地区,因为表中不存在这样一条记录.相反,having子句可以让我们筛选成组后的各组数据