您的位置:首页 > 数据库 > MySQL

mysql 存储过程个人总结

2013-05-15 15:00 190 查看
调用存储过程

call Mobile_Active(_userid);  

组合值 

concat('您的好友',a.toTrueName,'已经成功注册')

 

算术运算符

+     加   SET var1=2+2;       4

-     减   SET var2=3-2;       1

*     乘   SET var3=3*2;       6

/     除   SET var4=10/3;      3.3333

DIV   整除 SET var5=10 DIV 3;  3

%     取模 SET var6=10%3 ;     1

比较运算符

>            大于 1>2 False

<            小于 2<1 False

<=           小于等于 2<=2 True

>=           大于等于 3>=2 True

BETWEEN      在两值之间 5 BETWEEN 1 AND 10 True

NOT BETWEEN  不在两值之间 5 NOT BETWEEN 1 AND 10 False

IN           在集合中 5 IN (1,2,3,4) False

NOT IN       不在集合中 5 NOT IN (1,2,3,4) True

=            等于 2=3 False

<>, !=       不等于 2<>3 False

<=>          严格比较两个NULL值是否相等 NULL<=>NULL True

LIKE         简单模式匹配 "Guy Harrison" LIKE "Guy%" True

REGEXP       正则式匹配 "Guy Harrison" REGEXP "[Gg]reg" False

IS NULL      为空 0 IS NULL False

IS NOT NULL  不为空 0 IS NOT NULL True
存储过程中的逻辑判断 if  else

IF ... THEN
    ...;
ELSE
    IF ... THEN
      ...;
    ELSEIF
      ...;
    ELSE
      ...;
    END IF;
END IF;

判断某个值是否存在

 if EXISTS( select DeviceID from UserCenter.`User_Device_Log` where UserID= _userid and ClientSoft like CONCAT('%',_productName,'%')) THEN

   call ScoreBuild(5,_userid);

   select TRUE into @r;

  else 

   select FALSE into @r;

  end if;

ClientSoft like CONCAT('%',_productName,'%')) 模糊查询用的 

 

MySql 里的IFNULL、NULLIF和ISNULL用法
今天用到了MySql里的isnull才发现他和MSSQL里的还是有点区别,现在简单总结一下:

mysql中isnull,ifnull,nullif的用法如下:

isnull(expr) 的用法:

如expr 为null,那么isnull() 的返回值为 1,否则返回值为 0。
mysql> select isnull(1+1);

-> 0

mysql> select isnull(1/0);

-> 1

使用= 的null 值对比通常是错误的。

isnull() 函数同 is null比较操作符具有一些相同的特性。请参见有关is null 的说明。

IFNULL(expr1,expr2)的用法:

假如expr1   不为   NULL,则   IFNULL()   的返回值为   expr1;

否则其返回值为   expr2。

IFNULL()的返回值是数字或是字符串,具体情况取决于其所使用的语境。

mysql>   SELECT   IFNULL(1,0);

->   1

mysql>   SELECT   IFNULL(NULL,10);

->   10

mysql>   SELECT   IFNULL(1/0,10);

->   10

mysql>   SELECT

IFNULL(1/0,'yes');

->   'yes'

IFNULL(expr1,expr2)的默认结果值为两个表达式中更加“通用”的一个,顺序为STRING、   REAL或

INTEGER。假设一个基于表达式的表的情况,     或MySQL必须在内存储器中储存一个临时表中IFNULL()的返回值:

CREATE   TABLE   tmp   SELECT   IFNULL(1,'test')   AS   test;

在这个例子中,测试列的类型为   CHAR(4)。
NULLIF(expr1,expr2)  的用法: 
如果expr1

=   expr2     成立,那么返回值为NULL,否则返回值为   expr1。

这和CASE   WHEN   expr1   =   expr2

THEN   NULL   ELSE   expr1   END相同。

mysql>   SELECT

NULLIF(1,1);

->   NULL

mysql>   SELECT   NULLIF(1,2);

->   1

如果参数不相等,则   MySQL   两次求得的值为     expr1   。

 

将值赋值给变量

CREATE    PROCEDURE `AchieveUser`(IN _userid int, IN _achievementid int)

begin

select Score ,Achievement into @score, @achievement   from User  where ID= _achievementid;    //将值赋值给变量  @score  @achievement  

insert into  Score_Build_Log (ItemID,ItemCode,UserID,Mobile,Score,ScoreExplain,CreateDate,Status)    //将值插入到数据库中

values (_achievementid,'',_userid,'',@score,@achievement,now(),1);

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: