mysql的字符拼接
在Mysql 数据库中存在两种字符串连接操作.具体操作如下
一. 语法:
1. CONCAT(string1,string2,…) 说明 : string1,string2代表字符串,concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
例1:
例2:
2. CONCAT_WS(separator,str1,str2,...) 注意:顺便说下在oracle中是没有concat_ws函数的,在oracle中只有concat拼接函数,还有就是用“||”这个符号实现字符串拼接。
说明 : string1,string2代表字符串,concat_ws 代表 concat with separator,第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
举例1:
mysql> select concat_ws('#','dbdh=','NorthEastTrcoon',null) AS dbdh_name_three;
+-----------------------+
| dbdh_name_three |
+-----------------------+
| dbdh=#NorthEastTrcoon |
+-----------------------+
1 row in set (0.00 sec)
例2:
mysql> select concat_ws(null,'dbdh=','NorthEastTrcoon',null) AS dbdh_name_fourth
;
+------------------+
| dbdh_name_fourth |
+------------------+
| NULL |
+------------------+
1 row in set (0.00 sec)
例3:
mysql> select concat_ws('*','dbdh=','NorthEastTrcoon',null) AS dbdh_name_fifth;
+-----------------------+
| dbdh_name_fifth |
+-----------------------+
| dbdh=*NorthEastTrcoon |
+-----------------------+
1 row in set (0.00 sec)
3. MySQL中group_concat函数
完整的语法如下:
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
基本查询
mysql> select * from stu1;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,逗号分隔(默认)
mysql> select id,group_concat(name) from aa group by id;
+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,分号分隔
mysql> select id,group_concat(name separator ';') from aa group by id;
+------+----------------------------------+
| id| group_concat(name separator ';') |
+------+----------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
+------+----------------------------------+
3 rows in set (0.00 sec)
以id分组,把去冗余的name字段的值打印在一行,
逗号分隔
mysql> select id,group_concat(distinct name) from aa group by id;
+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
mysql> select id,group_concat(name order by name desc) from aa group by id;
+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)
还有一个简单的连接方式为: ||
mysql> select * from student;
+----+------+-------+----------+------------+
| id | age | score | name | birth |
+----+------+-------+----------+------------+
| 1 | 23 | 78 | 李四 | 2017-10-10 |
| 2 | 24 | 78 | zhangsan | 2017-10-10 |
| 3 | 25 | 99 | 王五 | 2016-05-17 |
+----+------+-------+----------+------------+
3 rows in set (0.00 sec)
mysql> select id+999,name,name+99,name+'999' from student;
+--------+----------+---------+------------+
| id+999 | name | name+99 | name+'999' |
+--------+----------+---------+------------+
| 1000 | 李四 | 99 | 999 |
| 1001 | zhangsan | 99 | 999 |
| 1002 | 王五 | 99 | 999 |
+--------+----------+---------+------------+
3 rows in set, 6 warnings (0.00 sec)
mysql的字符拼接的更多相关文章
- ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法
ubuntu下修改mysql默认字符编码出现的Job failed to start解决办法 前几天卸掉了用了好多年的Windows,安装了Ubuntu12.04,就开始各种搭环境.今天装好了MySQ ...
- MySQL的字符编码体系(二)——传输数据编码
MySQL的字符编码体系能够分成两部分:一部分是关于数据库server本身存储数据表时怎样管理字符数据的编码:还有一部分是关于client与数据库server数据传输怎样编码.上一篇MySQL的字符编 ...
- Mysql 的字符编码机制、中文乱码问题及解决方案【转载】
本文转载自:http://hi.baidu.com/huabinyin/item/7f51e462df565c97c4d24929.感谢作者及相关博主. 相信很多朋友都会对字符编码敬而远 ...
- 拨开字符编码的迷雾--MySQL数据库字符编码
拨开字符编码迷雾系列文章链接: 拨开字符编码的迷雾--字符编码概述 拨开字符编码的迷雾--编译器如何处理文件编码 拨开字符编码的迷雾--字符编码转换 拨开字符编码的迷雾--MySQL数据库字符编码 1 ...
- 在oracle中,group by后将字符拼接,以及自定义排序
1.在oracle中,group by后将字符拼接.任务:在学生表中,有studentid和subject两个字段.要求对studentid进行group by分组,并将所选科目拼接在一起.oracl ...
- mysql根据字符截取字符串(总结)
mysql根据字符截取字符串(总结) 1.1 前言 为结合自己平常查资料的习惯,我会先给出例子,然后再对相关知识进行详解.该案例使用到的函数为:SUBSTRING_INDEX 1.2 需要实现的实 ...
- Linux下修改MySQL数据库字符编码为UTF-8解决中文乱码
由于MySQL编码原因会导致数据库出现乱码. 解决办法: 修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 &g ...
- 【Java】字符拼接成字符串的注意点
这两天敲代码的时候,偶然间发现一个好玩的事情,分享一下,记录一下. 该段代码主要是:先产生的几个整数,把整数转换成对应的字符,最后的字符拼接成字符串,在把字符拼接成字符串的时候,个人因为偷懒使用+号进 ...
- 修改mysql的字符集和默认存储引擎
转自:http://blog.csdn.net/wyzxg/article/details/8779682 author:skatetime:2012/05/18 修改mysql的字符集和默认存储引擎 ...
随机推荐
- Educational Codeforces Round 57 (Rated for Div. 2)
我好菜啊. A - Find Divisible 好像没什么可说的. #include<cstdio> #include<cstring> #include<algori ...
- 筛选出sql 查询结果中 不包含某个字符
select * from table1 where patindex('%关键字%' , aa) = 0 select * from table1 where charindex('关键字' , a ...
- Unity3D学习笔记(十九):UGUI、Image、Text、Button
UGUI:Unity官方最新,与NGUI同源 UI:User Interface(用户的操作界面),图片+文字 UGUI的组件: 1.创建UGUI组件时,会默认创建Canvas(画布)和EventSy ...
- echart提示框内容数据添加单位
本文为博主原创,转载须注明转载地址: 方法为: tooltip : { trigger: 'axis', formatter: '{a0}:{c0}%' }, legend: { data:['测试' ...
- HDU 6156 Palindrome Function
http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:$f(n,k)$表示判断n在k进制下是否是回文串,如果是,则返回k,如果不是,则返回1.现在要计算$ ...
- 在SSM框架中,multfile转file
import org.apache.commons.fileupload.disk.DiskFileItem; import org.springframework.web.multipart.Mul ...
- poj 2762 Going from u to v or from v to u? trajan+拓扑
Going from u to v or from v to u? Description In order to make their sons brave, Jiajia and Wind t ...
- shell :
示例一.(用作注释,占位)#!/bin/bash : this is single line comment : 'this is a multiline comment, second line e ...
- 音视频学习系列第(三)篇---wav文件的存储和解析
音视频系列 什么是wav wav是一种无损的音频文件格式,wav文件有两部分,第一部分是文件头,记录一些重要的参数信息,如音频的采样率,通道数,数据位宽,第二部分是数据部分,数据部分可以是PCM,也可 ...
- CentOS下的Autoconf和AutoMake(实践篇) 2
阅读过<Linux下的Autoconf和AutoMake(理论篇)>之后,进入到实践环节.实验环境:CentOS release 6.7 (Final) x64 1.检查一下这4个工具是否 ...