zoukankan      html  css  js  c++  java
  • mysql 字符串函数、分组函数

    字符串函数

    1、concat 函数

    drop table test;
    create table test(id int(4), name varchar(10), sex char(2));
    insert into test values(1, 'Tom', '男');
    select concat(id, name, sex) from test; //查询结果:1Tom男
    select concat(id, '-', name, sex) from test; //查询结果:1-Tom男
    update test set sex = null;
    select concat(id, name, sex) from test; //结果为null, 有一个为null, 结果为null

    2、concat_ws 函数

    select concat_ws('-', id, name, sex) from test; //结果为1-Tom
    第一个参数是其它参数的分隔符;即使有一个结果为null,其它的不为null,结果就不会为null

    3、group_concat 函数

    drop table test;
    create table test (name varchar(10), id int(4));
    insert into test values('天',1),('道',2),('酬',3),('勤',4);
    insert into test values('天',1),('道',2),('酬',3),('勤',4);
    select id, group_concat(name) from test group by id; // 默认逗号分隔
    1 天,天
    2 道,道
    3 酬,酬
    4 勤,勤
    select id, group_concat(name, '_') from test group by id;
    1 天_,天_
    2 道_,道_
    3 酬_,酬_
    4 勤_,勤_
    select id, group_concat(name separator'_') from test group by id;
    1 天_天
    2 道_道
    3 酬_酬
    4 勤_勤
    select id, group_concat(distinct name order by name desc separator '_') from test group by id;
    1 天
    2 道
    3 酬
    4 勤


    分组函数

     select cate_name as catename,count(cate_name) as count from `intl_order_goods` where order_id = 1 group by cate_name

    执行结果:

  • 相关阅读:
    同行——冲刺集合!!!
    团队作业第六次——事后诸葛亮
    同行——冲刺总结
    同行——冲刺day7(11.14)
    同行——测试随笔
    同行——冲刺day6(11.13)
    同行——冲刺day5(11.12)
    同行——冲刺day4(11.11)
    同行——冲刺day3(11.10)
    同行——冲刺day2(11.9)
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/9308153.html
Copyright © 2011-2022 走看看