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

    执行结果:

  • 相关阅读:
    织梦CMS去广告方法 for DedeCMS V5.7
    织梦网站底部的Power by DedeCms怎么去掉?
    java环境变量最佳配置
    HTML课上小结
    PHP四个阶段目标以及第一阶段学习内容
    例子:选项卡和进度条
    例子:轮播效果
    例子:下拉列表
    document对象操作:浏览器页面文件
    Windows对象操作:浏览器窗口信息
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/9308153.html
Copyright © 2011-2022 走看看