zoukankan      html  css  js  c++  java
  • mysql

    -- mysql if表达式
    -- if(expr1, expr2, expr3):expr1为true,返回expr2,反之expr3
    select user_email, if(user_email='gsdhj@qq.com', 'qq', '360') as result from `user` limit 2;
    
    -- ifnull(expr1, expr2):在 expr1 的值不为 NULL的情况下都返回 expr1,否则返回 expr2,如下:
    select ifnull('', '为null返回指定值') as `title`;
    select ifnull(null, '为null返回指定值') as `title`;
    
    -- nullif(expr1, expr2):如果两个参数相等则返回NULL,否则返回第一个参数的值expr1
    select nullif(user_party, user_nation) as result from `user` limit 10;
    select nullif('1', '1') as result from `user` limit 10;
    
    -- if then else 逻辑运算
    -- 在sql语句中,有两种形式
    -- 1、simple case
    select 
    	case user_party 
    		when 1 then '汉族'
    		when 2 then '满族'
    		when 3 then '蒙古族'
    		else '新疆维吾尔族'
    	end,
    	user_name
    from `user` limit 10;
    
    -- 2、searched case
    select 
    	case 
    		when user_nation = 1 then '群众1'
    		when user_nation = 2 then '群众'
    		else '无'
    	end,
    	user_name
    from `user` limit 10;
    
    
    -- if else 流程控制语句,在存储过程中使用
    delimiter $
    drop procedure if exists pro2;
    create procedure pro2(
    	param int
    )
    begin
    	set param = ifnull(param, 1);
    	if param=1 then
    		select * from `user` where user_id = 10;
    	elseif param=2 then
    		select * from `user` where user_id = 20;
    	else
    		select * from `user` where user_id = 30;
    	end if;
    end;
    $
    delimiter ;
    call pro2(null);
    call pro2(3);
    
    
    -- user_gender 从1【男】2【女】3【未知】 从1开始的
    select elt(user_gender, '男', '女', '未知') from `user` limit 10;
    

      

  • 相关阅读:
    暑假第二周总结
    7.18-7.24 第一周周报
    poj 3295 Tautology
    2016多校 #2 1006 Fantasia
    codeforces 698B Fix a Tree
    codeforces 699B Bomb
    HDU 4578(线段树
    CF 600F( 二分图
    hdu 5517 Triple(二维树状数组)
    HDU HDOJ5412(树套树or整体二分
  • 原文地址:https://www.cnblogs.com/gygtech/p/13666573.html
Copyright © 2011-2022 走看看