mysql的查詢結果行欄位拼接,能夠用以下兩個函式實現:
1. concat函式
mysql> select concat('1','2','3') from test ;
| concat('1','2','3') |
| 123 |
假設連線串中存在null,則返回結果為null:
mysql> select concat('1','2',null,'3') from test ;
| concat('1','2',null,'3') |
| null |
2. concat_ws函式
concat(separator,str1,str2,...) 代表 concat with separator ,是concat()的特殊形式。第乙個引數是其他引數的分隔符。分隔符的位置放在要連線的兩個字串之間。分隔符能夠是乙個字串,也能夠是其他引數。
mysql> select concat_ws(':','1','2','3') from test ;
| concat_ws(':','1','2','3') |
| 1:2:3 |
分隔符為null,則返回結果為null:
mysql> select concat_ws(null,'1','2','3') from test;
| concat_ws(null,'1','2','3') |
| null |
假設引數中存在null,則會被忽略:
mysql> select concat_ws(':','1','2',null,null,null,'3') from test ;
| concat_ws(':','1','2',null,null,null,'3') |
| 1:2:3 |
能夠對null進行推斷,並用其他值進行替換:
mysql> select concat_ws(':','1','2',ifnull(null,'0'),'3') from bank limit 1;
| concat_ws(':','1','2',ifnull(null,'0'),'3') |
| 1:2:0:3 |
GROUP BY 兩個字段
create table test a varchar 10 b varchar 10 c int insert into test values a 甲 1 insert into test values a 甲 1 insert into test values a 甲 1 insert int...
mysql 如何把兩個字段拼接起來
在mysql中有個關鍵字 concat ws 關鍵字解釋 根據特定的字元將不同的字段拼接在一起 例如 表 student id age name 1 2 xiaohong 2 4 xiaoming 拼接 concat ws xiaoming xiaohong as total 拼接結果 total ...
MySQL之兩個欄位的in
employee 表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 departmen...