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
执行结果:

mysql 字符串函数、分组函数的更多相关文章
- Oracle_SQL函数-分组函数
分组函数 什么是分组函数 分组函数作用于一组数据,并对一组数据返回一个值 组函数类型:主要有6种 AVG - 平均 COUNT - 计数 MAX - 最大 MIN - 最小 SUM - 求和 STDD ...
- MySQL 字符串连接CONCAT()函数
MySQL字符串连接函数 使用方法:CONCAT(str1,str2,-) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串, ...
- MySQL 字符串截取SUBSTRING()函数
MySQL 字符串截取相关函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例: select left(content,200) as ab ...
- MySQL☞聚合函数/分组函数
分组函数(聚合函数) 1.count(*/列名): a.*:求出该数据的总条数 select count(*) from 表名 b.列名:求出该列中列名不为null的总条数 select cou ...
- mysql字符串的常用函数(截取和拼接)
#截取字符串(先正序取2个,再倒序取1个)SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('aaa-gg-cc-dd','-',2),'-',-1) #获取子表某个字段的 ...
- 025、MySQL字符串大小写转化函数,文本转化大写,文本转化小写
#变大写 SELECT UPPER('abcdABCD123a'); #ABCDABCD123A SELECT UCASE('abcdABCD123a'); #ABCDABCD123A #变小写 SE ...
- mysql基础教程(二)-----分组函数、多表查询、常见函数
分组函数 什么是分组函数 分组函数作用于一组数据,并对一组数据返回一个值. 组函数类型 • AVG() • COUNT() • MAX() • MIN() • SUM() 组函数语法 AVG(平均值) ...
- oracle 分组函数、视图
组函数 分组函数作用于一组数据,对每一组返回一个值 组函数类型: 1.计数 count(列名 或 表达式) 对满足的行数进行统计 2.求和 sum(列名 或 表达式 ...
- oracle_经常使用分组函数
oracle_经常使用分组函数 ①分组函数 1.max(column):求最大值,对数据类型没有要求,随意数据类型都能够 2.min(column):求最小值,对数据类型没有要求,随意数据类型都 ...
随机推荐
- django第二课 网页继承
第一步 创建项目(有问题可以看我的第一个博客) C:\Python36\Scripts\django-admin.py startproject *** (我的写法,仅供参考) 第二步 创建文件夹,同 ...
- Elasticsearch Aggregation 多个字段分组统计 Java API实现
现有索引数据: index:school type:student --------------------------------------------------- {"grade&q ...
- centos6 和 centos7 网络配置
centos 6配置,1 vim /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE="eth0" BOOTPROTO="st ...
- Jmeter参数化与检查点
一.Jmeter参数话有3种方法: 1. add->pre processors->user parameter(用户参数) 2.add->config Element->CS ...
- Android 开发工具类 14_ JsonTools
天气 JSON 数据解析 package com.example.weather_json.tools; import java.util.ArrayList; import java.util.Li ...
- golang---interface结合reflect的泛型应用
大致思路:序列化未知json放入一个interface{}中再通过反射将其内容解析出来 str1:=`{ , , , , , , "type_key": "testnow ...
- 数据库学习---SQL基础(二)
数据库学习---SQL基础(一) 数据库学习---SQL基础(二) 数据库学习---SQL基础(三) 上篇复习的sql的增删改查,and ,or ,>=, <=,!=等逻辑运算符,还有in ...
- Spring中使用两种Aware接口自定义获取bean
在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic clas ...
- 鼠标样式——css国际组织
w3c国际标准组织提供的鼠标样式: http://css-cursor.techstream.org/
- 二进制GCD算法 减少%的时间消耗
/* 二进制求最大公约数.由于传统的GCD,使用了%,在计算机运行过程中要花费大量的时间,所以,采取二进制的求法,来减少时间的消耗. 算法: 当a,b都是偶数时: gcd(a,b)=2*gcd(a/2 ...