MySQL字符串的拼接方法
生活随笔
收集整理的這篇文章主要介紹了
MySQL字符串的拼接方法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、CONCAT(str1,str2,str3, …)
示例:
mysql> select concat("我愛你","何晶","一萬年"); +------------------------------------------+ | concat("我愛你","何晶","一萬年") | +------------------------------------------+ | 我愛你何晶一萬年 | +------------------------------------------+ 1 row in set (0.00 sec)mysql>-- 拼接 null 字符串 mysql> select concat("我愛你","何晶","一萬年",'null'); +-------------------------------------------------+ | concat("我愛你","何晶","一萬年",'null') | +-------------------------------------------------+ | 我愛你何晶一萬年null | +-------------------------------------------------+ 1 row in set (0.00 sec)mysql>-- 拼接 nullmysql> select concat("我愛你","何晶","一萬年",null);;; +-----------------------------------------------+ | concat("我愛你","何晶","一萬年",null) | +-----------------------------------------------+ | NULL | +-----------------------------------------------+ 1 row in set (0.00 sec)mysql>二、CONCAT_WS(separator,str1,str2,…)
說明 :
string1,string2代表字符串,concat_ws 代表 concat with separator,第一個(gè)參數(shù)是其它參數(shù)的分隔符。 分隔符的位置放在要連接的兩個(gè)字符串之間。 分隔符可以是一個(gè)字符串,也可以是其它參數(shù)。 如果分隔符為 NULL,則結(jié)果為 NULL。 函數(shù)會(huì)忽略任何分隔符參數(shù)后的 NULL 值示例:
mysql> select concat_ws("何晶","我愛你","一萬年"); +---------------------------------------------+ | concat_ws("何晶","我愛你","一萬年") | +---------------------------------------------+ | 我愛你何晶一萬年 | +---------------------------------------------+ 1 row in set (0.00 sec)mysql>-- 分隔符為 null mysql> select concat_ws(null,"我愛你何晶","一萬年");;; +-----------------------------------------------+ | concat_ws(null,"我愛你何晶","一萬年") | +-----------------------------------------------+ | NULL | +-----------------------------------------------+ 1 row in set (0.00 sec)mysql>-- 被拼接的字符串為 null (null的位置不在第一位) mysql> select concat_ws("一萬年","我愛你何晶",null); +-----------------------------------------------+ | concat_ws("一萬年","我愛你何晶",null) | +-----------------------------------------------+ | 我愛你何晶 | +-----------------------------------------------+ 1 row in set (0.00 sec)mysql>-- 被拼接的字符串為 null (null的位置在第一位) mysql> select concat_ws("一萬年",null,"我愛你何晶"); +-----------------------------------------------+ | concat_ws("一萬年",null,"我愛你何晶") | +-----------------------------------------------+ | 我愛你何晶 | +-----------------------------------------------+ 1 row in set (0.00 sec)mysql>三、 MySQL中 GROUP_CONCAT 函數(shù)
完整語法
group_concat([DISTINCT] 要連接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])示例:
-- 基礎(chǔ)查詢 sc表 mysql> select * from sc limit 10; +-------+------+------+-------+ | sc_id | s_id | c_id | score | +-------+------+------+-------+ | 1 | 1 | 1 | 23 | | 2 | 1 | 2 | 93 | | 3 | 1 | 3 | 55 | | 4 | 1 | 4 | 96 | | 5 | 1 | 5 | 15 | | 6 | 1 | 6 | 88 | | 7 | 1 | 7 | 91 | | 8 | 1 | 8 | 94 | | 9 | 1 | 9 | 95 | | 10 | 1 | 10 | 92 | +-------+------+------+-------+ 10 rows in set (0.00 sec)-- 以 c_id 分組,把 score 字段的值打印在一行,逗號(hào)分隔(默認(rèn)) mysql> select c_id,group_concat(score) from sc where sc_id < 20 group by c_id limit 10; +------+---------------------+ | c_id | group_concat(score) | +------+---------------------+ | 1 | 23,73 | | 2 | 93,90 | | 3 | 55,29 | | 4 | 96,75 | | 5 | 15,87 | | 6 | 88,9 | | 7 | 91,86 | | 8 | 94,100 | | 9 | 95,42 | | 10 | 92 | +------+---------------------+ 10 rows in set (0.00 sec)mysql>-- 以 c_id 分組,把 score 字段的值打印在一行,分號(hào)分隔 mysql> select c_id,group_concat(score separator ";") from sc where sc_id < 20 group by c_id limit 10; +------+-----------------------------------+ | c_id | group_concat(score separator ";") | +------+-----------------------------------+ | 1 | 23;73 | | 2 | 93;90 | | 3 | 55;29 | | 4 | 96;75 | | 5 | 15;87 | | 6 | 88;9 | | 7 | 91;86 | | 8 | 94;100 | | 9 | 95;42 | | 10 | 92 | +------+-----------------------------------+ 10 rows in set (0.00 sec)mysql>-- 以 sc_id 分組,把去冗余的 score 字段的值打印在一行 mysql> select c_id,group_concat(distinct score) from sc where sc_id < 120 group by c_id limit 10; +------+-------------------------------------+ | c_id | group_concat(distinct score) | +------+-------------------------------------+ | 1 | 13,15,22,23,32,39,62,71,73,79,83,86 | | 2 | 9,15,20,46,49,50,52,54,68,75,90,93 | | 3 | 9,14,15,20,29,35,55,56,65,80,87,98 | | 4 | 13,18,34,35,37,50,53,73,75,77,88,96 | | 5 | 7,15,21,23,24,43,63,66,69,87 | | 6 | 8,9,25,27,28,52,55,60,73,76,81,88 | | 7 | 8,11,18,20,29,35,67,84,86,91,100 | | 8 | 6,12,24,26,73,76,77,84,87,94,95,100 | | 9 | 17,32,39,42,45,50,58,67,76,95,97 | | 10 | 4,8,19,27,33,52,53,61,78,86,92 | +------+-------------------------------------+ 10 rows in set (0.00 sec)mysql>-- 以sc_id分組,把 score 字段的值打印在一行,逗號(hào)分隔,以 score 排倒序 mysql> select c_id,group_concat(score order by score desc) from sc where sc_id < 120 group by c_id; +------+-----------------------------------------+ | c_id | group_concat(score order by score desc) | +------+-----------------------------------------+ | 1 | 86,83,79,73,71,62,39,32,23,22,15,13 | | 2 | 93,90,75,68,54,52,50,49,46,20,15,9 | | 3 | 98,87,80,65,56,55,35,29,20,15,14,9 | | 4 | 96,88,77,75,73,53,50,37,35,34,18,13 | | 5 | 87,69,66,63,43,24,23,23,21,15,7,7 | | 6 | 88,81,76,73,60,55,52,28,27,25,9,8 | | 7 | 100,91,86,84,67,35,29,20,18,11,11,8 | | 8 | 100,95,94,87,84,77,76,73,26,24,12,6 | | 9 | 97,95,76,67,58,58,50,45,42,39,32,17 | | 10 | 92,86,78,61,53,52,33,27,19,8,4 | +------+-----------------------------------------+ 10 rows in set (0.01 sec)mysql>四、直接 + (其實(shí)都不算是拼接了,算是相加)
示例:
mysql> select "n1m1"+999; +------------+ | "n1m1"+999 | +------------+ | 999 | +------------+ 1 row in set, 1 warning (0.00 sec)mysql> select "n1m1"+"999"; +--------------+ | "n1m1"+"999" | +--------------+ | 999 | +--------------+ 1 row in set, 1 warning (0.00 sec)mysql> select "1"+"999"; +-----------+ | "1"+"999" | +-----------+ | 1000 | +-----------+ 1 row in set (0.00 sec)mysql> select 1+"999"; +---------+ | 1+"999" | +---------+ | 1000 | +---------+ 1 row in set (0.00 sec)mysql> select "asf"+"mimi"; +--------------+ | "asf"+"mimi" | +--------------+ | 0 | +--------------+ 1 row in set, 2 warnings (0.00 sec)總結(jié)
以上是生活随笔為你收集整理的MySQL字符串的拼接方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows 使用 tree 命令显示
- 下一篇: php mysql登陆页面完整代码_PH