10.1、变量、运算符介绍

用户变量:使用@开头。【set定义,会话有效】
系统变量:使用@@开头。【declare定义,begin..end有效】
局部变量: 在存储过程中有效。

-- 1、用户变量使用方法
set @sno=95001;
select @sno; -- 查询设置的用户变量

-- 设置变量且定义默认值
select @name:='张三';

-- 查询并且设置值
select sname into @name from student where sno=@sno;
select @name;

-- 2、局部变量
delimiter $$
create procedure proc_add(in a int,in b int)
begin
    -- 局部变量
    declare c int default 0;
    set c=a+b;
    select c as 'result';
end$$
delimiter ;
call proc_add(2,3);

-- 3、系统变量:session变量和gobal变量
set @@wait_timeout=10000; --会话变量
set SESSION wait_timeout=10000; --会话变量
set @@global.wait_timeout=10000; --全局变量
set GLOBAL wait_timeout=10000; --全局变量

select @@wait_timeout -- 查询设置值

-- 查询所有会话变量
show session variables;

-- 查询所有全局变量
show global variables;

-- 4、常量:指的就是值不变的变量

-- 5、字符串常量
-- 5、数值常量
-- 6、日期时间常量
-- 7、布尔常量
-- 8、NULL常量

-- 9、运算符和表达式
运算符是执行数学运算﹑字符串连接以及列﹑常量和变量之间进行比较的符号。
9.1、算术运算符: +-*/%
9.2、赋值运算符:=、:=
9.3、逻辑运算符: !( NOT ) 、&&(AND ) 、||( OR ) 、XOR
9.4、位运算符: &^<<>> ﹑~
9.3、比较运算符: =<>(!=) 、<=><<=>>=IS NULL
【例】运算符和表达式使用示例
SET @x= 5,@y = 3;
SET @x=@x +@y;
SELECT @x;

10.2、流程控制

10.2.1、if

-- 如果有填写则显示,没有填写则显示0
select sname,if(age,age,'0') from student;

10.2.2、ifnull

-- 双目运算符,如果字段为null,则显示指定的内容
select sname,ifnull(birthday,'无填写') from student;

10.2.3、if…else【只能用于存储过程】

-- if...else 判断年龄是否成年人
delimiter $$
create procedure query_age(in var_sno int)
begin
declare temp int;
select age into temp from student where sno=var_sno;
if temp>=18 then
    select '成年人';
else
    select '未成年人';
end if;
end$$
delimiter ;

10.2.4、if…elseif…else

-- 判断成绩等级
drop procedure query_age;
delimiter $$
create procedure query_age(in var_sno varchar(32))
begin
declare temp int;
declare grade_desc varchar(32);
select score into temp from student where sno=var_sno;
if temp<60 then
    set grade_desc='不及格';
elseif temp>=90 then
    set grade_desc='优秀';
elseif temp>=60 and temp<=79 then
    set grade_desc='及格';
elseif temp>=80 and temp<=89 then
    set grade_desc='良好';
else
    set grade_desc='未知';
end if;
select grade_desc;
end$$
delimiter ;
call query_age('95005');

10.2.5、case

-- case 性别:男为1,女为0
select sname,ssex,
case ssex
when '' then 1
when '' then 0
else '未知'
end as ssex_num
from student;

10.3、循环语句

10.3.1、while

-- while 输入1和100的和
delimiter $$
create procedure doWhile()
begin
declare count int default 1;
declare sum int default 0;
while count<=100 do
    set sum=sum+count;
    set count=count+1;
end while;
select sum;
end$$
delimiter ;

10.3.2、loop、leave

-- loop 输入1和100的和
-- leave 相当于break
delimiter $$
create procedure doWhile()
begin
declare count int default 1;
declare sum int default 0;
add_num:loop
    set sum=sum+count;
    if count>=100 then
        leave add_num;
    end if;
    set count=count+1;
end loop add_num;
select sum;
end$$
delimiter ;

10.3.3、loop、iterate 

-- loop 输入1和100能被3整除的和
-- leave 相当于break
-- iterate 相当于continue
drop procedure doWhile;
delimiter $$
create procedure doWhile()
begin
declare count int default 1;
declare sum int default 0;
add_num:loop
    set count=count+1;    
    if count=100 then
        leave add_num;
    else if mod(count,3)=0 then
        iterate add_num;
        end if;
    end if;
    set sum=sum+count;
end loop add_num;
select sum;
end$$
delimiter ;
call doWhile();

10.3.4、repeat

-- repeat 输入1和100能被3整除的和
drop procedure doWhile;
delimiter $$
create procedure doWhile()
begin
declare count int default 0;
declare sum int default 0;
add_num:repeat
    set count=count+1;    
    set sum=sum+count;
    until count>=100
end repeat add_num;
select sum;
end$$
delimiter ;
call doWhile();

10.4、实践:利用循环,批量插入1000条数据

create table user(
    id int primary key auto_increment,
    uname varchar(32)
);

drop procedure add_user;
delimiter $$
create procedure add_user()
begin
declare count int default 1;
while count<=1000 do
insert into user(uname) values(concat('zhangsan',cast(count as unsigned)));
    set count=count+1;
end while;
end$$
delimiter ;
call add_user();

 

原文地址:http://www.cnblogs.com/ygbh/p/16923745.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性