mysql concat(str1,str2...)连接两个字符串,(数字也是可以的,会转成字符串)

MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL

mysql> select concat('11','22',null);
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL   |
+------------------------+
1 row in set (0.00 sec)
concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接
如连接后以逗号分隔
mysql> select concat_ws(',','11','22','33');
 
+-------------------------------+
| concat_ws(',','11','22','33') |
+-------------------------------+
| 11,22,33                      |
+-------------------------------+
1 row in set (0.00 sec)
现在有这样一个需求,
要查找用户名和密码为(user1,pwd1),(user2,pwd2),(user2,plwd3)
由于字段与字段之间有关联,我们就可以使用连接解决这个问题:
select * from user where concat(username,'-',pwd) in ('user1-pwd1','user2-pwd2')
 
group_concat()可用来行转列, Oracle没有这样的函数
 
完整的语法如下
group_concat([DISTINCT] 要连接的字段[Order BY ASC/DESC 排序字段] [Separator '分隔符'])
如下例子
mysql> select * from aa;
 
+------+------+
| id   | name |
+------+------+
|    1 | 10   |
|    1 | 20   |
|    1 | 20   |
|    2 | 20   |
|    3 | 200  |
|    3 | 500  |
+------+------+
6 rows in set (0.00 sec)
3.1 以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)
 
3.2 以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)
 
3.3 以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)
 
3.4 以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)
 
4、repeat()函数,用来复制字符串,如下'ab'表示要复制的字符串,2表示复制的份数
 
mysql> select repeat('ab',2);
 
+----------------+
| repeat('ab',2) |
+----------------+
| abab           |
+----------------+
 
1 row in set (0.00 sec)
 
又如
mysql> select repeat('a',2);
 
+---------------+
| repeat('a',2) |
+---------------+
| aa            |
+---------------+
1 row in set (0.00 sec)

mysql concat和group_concat的更多相关文章

  1. MySQL CONCAT()与GROUP_CONCAT()的使用

      1 . MySQL CONCAT(str1,str2, ...)  --返回连接的字符串 mysql> SELECT CONCAT('My', 'S', 'QL'); -> 'MySQ ...

  2. mysql中的concat,concat_ws(),group_concat()

    mysql中的concat,concat_ws(),group_concat() 说明: 本文中使用的例子均在下面的数据库表tt2下执行:     一.concat()函数 1.功能:将多个字符串连接 ...

  3. mysql 多行(GROUP_CONCAT)和多列(CONCAT)的合并函数

    1,多行合并:把查询的一行或者多行进行合并. SELECT GROUP_CONCAT(md.data1) FROM DATA md,contacts cc WHERE md.conskey=cc.id ...

  4. 浅析MySQL中concat以及group_concat的使用

      说明: 本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接 ...

  5. [转]MySQL中函数CONCAT及GROUP_CONCAT

    一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +-- ...

  6. Mysql学习笔记—concat以及group_concat的用法(转载)

    本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字 ...

  7. mysql函数concat与group_concat使用说明

    mysql函数concat与group_concat使用说明concat()函数<pre>mysql> select concat(',',name,',') from `user` ...

  8. ThinkPHP第十一天(关联模型使用,独立分组配置,MySQL concat用法)

    1.关联模型的使用 定义方式:新建一个类文件UserRelationModel.class.php Class UserRelationModel extends RelationModel{ pro ...

  9. 连接列值 mysql CONCAT函数

    mysql concat(str1,str2,...) 返回结果为连接参数产生的字符串,如果任何一个参数带有null,则返回结果为null.如果所有参数均为非二进制字符串,则结果为非二进制字符串. 如 ...

随机推荐

  1. doctype的种类

    现实生活中人们使用的浏览器是各式各样的!为了防止各各浏览器页面设置不一样而照成我们所做的网页乱序,不得不设置doctye(文档类型):doctype有3中类型:* 过渡的(Transitional): ...

  2. angular $q服务的用法

    Promise是一种和callback有类似功能却更强大的异步处理模式,有多种实现模式方式,比如著名的Q还有JQuery的Deffered. 什么是Promise 以前了解过Ajax的都能体会到回调的 ...

  3. zhuan:windows用一键安装包安装(推荐)-禅道

    访问地址:http://www.zentao.net/book/zentaopmshelp/76.html 一键安装包 解压缩必须 解压缩到根目录下面.

  4. FIFO、LRU、OPT这三种置换算法的缺页次数

    考虑下述页面走向: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6 当内存块数量分别为3时,试问FIFO.LRU.OPT这三种置换算法的缺页次数各是多少? 答:缺页定义 ...

  5. 1027 Colors in Mars (20)

    #include <stdio.h> #include <map> using namespace std; int main() { int R,G,B,i; map< ...

  6. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. (转)Qt Model/View 学习笔记 (三)——Model类

    Model类 基本概念 在model/view构架中,model为view和delegates使用数据提供了标准接口.在Qt中,标准接口QAbstractItemModel类中被定义.不管数据在底层以 ...

  8. 在fedora 桌面上添加应用程序

    在网上下了个android studio,这个程序只是的压缩包,解压后程序也只能在SHELL下敲入studio.sh才能运行 能不能向其他程序一样,在fedora桌面上找到应用程序,点击执行呢.在网上 ...

  9. 【面试题】Https

    https原理, 我很难一下子记住,   https=http+ssl 先说http,  基于tcp/IP协议 传输,有三次握手 http://blog.csdn*net/xubo_zhang/art ...

  10. EXTJS 3.0 资料 控件之 FormPanel 插入button用法

    最近发现项目中FormPanel 里面需要 增加 button,的模块比较多,具体代码如下: var eastPanelForm_Dele = new Ext.form.FormPanel({ id: ...