mysql拼接函式

yy418408247發表於2016-05-27

1.repeat()函式
使用方法:
REPEAT(str,count)  str:字串或者是對應的值;count:幾個數值,就是前面字串的個數。
例子一: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)
解析:用來複制字串,如下'ab'表示要複製的字串,後面的數字,如3表示複製的份數。


2. group_concat 函式
完整的語法如下:
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欄位的值列印在一行,逗號分隔(預設)
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)

3.RTrim()函式
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()去掉串左右兩邊的空格。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29494175/viewspace-2107706/,如需轉載,請註明出處,否則將追究法律責任。

相關文章