mysql编程语法_MySQL基础~~编程语法

常量

数值

字符串:单引号或者双引号括起来。包括普通字符串或者日期格式的字符串。

布尔值:false(FALSE)对应数字值为0、true(TRUE)对应数字值为1。

变量

定义用户变量

mysql中变量分为用户变量和系统变量。

用户变量要先定义和初始化赋值,否则变量的值为NULL。

用户变量只在本次连接阶段有效,其他用户的连接不能使用另外一个用户定义的变量,并且当连接释放后,变量就会销毁。

声明变量格式:set @key = value,可以一次性声明多个。

如果变量名中有特殊符号,那么可以用引号将变量名括起来,比如 set @'abc def' = 123;

访问用户变量

select @name;

系统变量

系统变量是以2个@@开头。

mysql> select @@version;

获得系统变量列表

mysql> show variables;

mysql> show variables like '%test%';

算数运算符

+ - * / %

+和-还可以用来计算日期;

mysql> select now(),now() + interval 22 day;

+---------------------+-------------------------+

| now() | now() + interval 22 day |

+---------------------+-------------------------+

| 2018-06-19 22:55:02 | 2018-07-11 22:55:02 |

+---------------------+-------------------------+

1 row in set (0.00 sec)

比较运算符

在mysql里面,判断等于只用一个等号=,不像其他编程语言一样使用双等或者三等。判断不等使用!=和<>。

其他比较运算符和其他编程语言一样使用。

逻辑运算符和位运算符

和其他语言一样。注意or比and的优先级低,所以在where子句中进行判断时,为了保证准确性,尽量使用()保证顺序。

选择判断

mysql的选择判断一般是使用case,格式如下:

case

when 条件1 then 表达式1

when 条件2 then 表达式2

else 表达式n

end

示例:

mysql> select id,name,

-> case

-> when price>10 then "expensive"

-> when price>0 then "cheap"

-> else "free"

-> end as level

-> from cate;

循环结构

while

delimiter $$

create procedure test_while()

begin

declare sum int default 0;

declare t int default 5;

while t>0 do

set sum=sum+1;

set t=t-1;

end while;

select sum;

end $$

delimiter ;

repeat

delimiter $$

create procedure _repeat()

begin

declare a int default 10;

repeat

set a=a-1;

until a<5

end repeat;

select a;

end $$

delimiter ;

注意使用repeat的时候,在判断条件(until 条件)的那一行句末不加分号,这个很容易出错!

loop

delimiter $$

create procedure test_loop()

begin

declare t int default 0;

label:loop

set t=t+1;

if t>10 then leave label;

end if;

end loop label;

select t;

end $$

delimiter ;

注意 loop 一般要和一个标签(此处为label,名称可以自定义,不过要保证前后一致)一起使用,且在 loop 循环中一定要有一个判断条件,能够满足在一定的条件下跳出 loop 循环(即 leave )!

存储过程

创建和使用存储过程

mysql> create procedure insert_into_two_table(in id int, in name char(30), out res char(10))

-> begin

-> insert into user values (id,name);

-> insert into person values (id, name);

-> set res = "success";

-> select res;

-> end $$

Query OK, 0 rows affected (0.00 sec)

mysql> call insert_into_two_table(1,"aaaaa",@res);

+---------+

| res |

+---------+

| success |

+---------+

1 row in set (0.03 sec)

mysql> select @res;

+---------+

| @res |

+---------+

| success |

+---------+

1 row in set (0.00 sec)

删除存储过程

mysql> drop procedure if exists insert_into_two_table;

存储函数

存储过程不能有输出参数;不用call调用,而是使用select来调用 ;必须包含return语句,存储过程不能有return。

delimiter $$

create function find_stu(s_id int)

returns boolean

begin

declare cnt int;

select id into cnt from user where id=s_id;

if cnt > 0 then

return true;

else

return false;

end if;

end $$

delimiter ;

测试:

mysql> select * from user;

+----+-------+

| id | name |

+----+-------+

| 1 | aaaaa |

+----+-------+

1 row in set (0.00 sec)

mysql> select find_stu(1);

+-------------+

| find_stu(1) |

+-------------+

| 1 |

+-------------+

1 row in set (0.00 sec)

mysql> select find_stu(2);

+-------------+

| find_stu(2) |

+-------------+

| 0 |

+-------------+

1 row in set (0.00 sec)

删除函数

mysql> drop function if exists find_stu;

触发器

事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值