MySQL中怎么拼接函數(shù),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司主營(yíng)拜城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件開(kāi)發(fā)公司,拜城h5成都微信小程序搭建,拜城網(wǎng)站營(yíng)銷推廣歡迎拜城等地區(qū)企業(yè)咨詢
1.repeat()函數(shù)
使用方法:
REPEAT(str,count) str:字符串或者是對(duì)應(yīng)的值;count:幾個(gè)數(shù)值,就是前面字符串的個(gè)數(shù)。
例子一:mysql>select repeat('ab',3);
+-------------------------------+
| repeat('ab',3) |
+-------------------------------+
| ababab |
+-------------------------------+
1 row in set (0.00 sec)
范例:mysql> select repeat(name,2) from table_name;
+-------------------------------+
| repeat(name,2) |
+-------------------------------+
| aaaa |
| bbbb |
| cccc |
+-------------------------------+
3 row in set (0.00 sec)
解析:用來(lái)復(fù)制字符串,如下'ab'表示要復(fù)制的字符串,后面的數(shù)字,如3表示復(fù)制的份數(shù)。
2. group_concat 函數(shù)
完整的語(yǔ)法如下:
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)
以id分組,把name字段的值打印在一行,逗號(hào)分隔(默認(rèn))
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字段的值打印在一行,分號(hào)分隔
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字段的值打印在一行,
逗號(hào)分隔
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字段的值打印在一行,逗號(hào)分隔,以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)
3.RTrim()函數(shù)
select concat(RTrim(name),'(',RTrim(country),')') from table_name order by name;
例子一:
select concat(LTRIM(' werrrt '),'(',RTRIM(2343),')');
+------+---------------------------------------------------+
| id |concat(LTRIM(' werrrt '),'(',TRIM( 2343 ),')') |
+------+---------------------------------------------------+
|1 |werrrt (2343) |
+------+---------------------------------------------------+
1 rows in set (0.00 sec)
解析:LTrim()去掉串左邊的空格。
例子二:
select concat(RTRIM(' werrrt '),'(',RTRIM(2343),')');
+------+---------------------------------------------------+
| id |concat(LTRIM(' werrrt '),'(',TRIM( 2343 ),')') |
+------+---------------------------------------------------+
|1 | werrrt(2343) |
+------+---------------------------------------------------+
1 rows in set (0.00 sec)
解析:RTrim()去掉串右邊的空格。Trim()去掉串左右兩邊的空格。
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。