mysql常用方法学习

环境

  

create table phople (
    id int(11) not null primary key auto_increment,
    name char(20) not null,
    sex int(1) not null default '0',
    degree double(16, 2)
);

insert into phople value (null, 'zh', '0', '0');

insert into phople value (null, 'az', '1', '22');
insert into phople value (null, 'aa', '1', '33');
insert into phople value (null, 'ad', '1', '44');

1. select group_concat(name) from phople;                      ## 直接把所有的name字段值链接起来。  

  运行结果: zh,az,aa,ad

    select group_concat(name) from phople group by sex;  ## 根据sex分组后。把分组的name字段相连接起来。

  运行结果: 

      zh

      az,aa,ad

     select group_concat(name,'||') from phople group by sex; ## 默认链接为 ','; 这里可以改变链接符号。

      运行结果:

    zh||
    az||,aa||,ad||

2. IFNULL方法

  MYSQL IFNULL(expr1,expr2)          
      如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。          

3. IF 方法

  IF(expr1,expr2,expr3)          
  如果expr1是TRUE(expr1<>0且expr1<>NULL),那么IF()返回expr2,否则它返回expr3。IF()返回一个数字或字符串值,取决于它被使用的上下文。          
  mysql> select IF(1>2,2,3);      
             -> 3

4. CONCAT(str1,str2,…)  字符串相连接的的函数。

    返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

5. FIND_IN_SET(str, strlist);

  mysql> select find_in_set('b', 'a,b,c');

  >2

  返回 b在'a,b,c'字符串中的第几个。注意的是 'a,b,c' 这个字符串必须是要用 ',' 分隔。否则返回0,无效。

  

原文地址:https://www.cnblogs.com/shaoshao/p/5666142.html