MySQL中的拼接Concat
在MySQL的SELECT语句中,可使用
Concat()函数来拼接两个列 .
Concat()拼接串,即把多个串连接起来形成一个较长的串.
Concat()需要一个或多个指定的串,
各个串之间用逗号分隔 .
Trim() 函数用来去掉
串左右两边的空格
LTrim() 函数用来去掉
串左边的空格
RTrim() 函数用来去掉
串右边的空格
mysql> update product set name=' 粉底液 ' where id='c007';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from product;
+
| id | name | price |
+
| c001 | 防晒霜 | 68 |
| c002 | 护手霜 | 23 |
| c003 | 面膜 | 79 |
| c004 | 粉饼 | 58 |
| c005 | 口红 | 59 |
| c006 | 水乳 | 68 |
| c007 | 粉底液 | 68 |
| c003 | 卸妆油 | NULL |
| c003 | NULL | NULL |
+
9 rows in set (0.01 sec)
mysql> select Concat(id,' (',name,')') from product;
+
| Concat(id,' (',name,')') |
+
| c001 (防晒霜) |
| c002 (护手霜) |
| c003 (面膜) |
| c004 (粉饼) |
| c005 (口红) |
| c006 (水乳) |
| c007 ( 粉底液 ) |
| c003 (卸妆油) |
| NULL |
+
9 rows in set (0.00 sec)
mysql> select Concat(id,name) from product;
+
| Concat(id,name) |
+
| c001防晒霜 |
| c002护手霜 |
| c003面膜 |
| c004粉饼 |
| c005口红 |
| c006水乳 |
| c007 粉底液 |
| c003卸妆油 |
| NULL |
+
9 rows in set (0.00 sec)
mysql> select Concat(id,' (',RTrim(name),')') from product;
+
| Concat(id,' (',RTrim(name),')') |
+
| c001 (防晒霜) |
| c002 (护手霜) |
| c003 (面膜) |
| c004 (粉饼) |
| c005 (口红) |
| c006 (水乳) |
| c007 ( 粉底液) |
| c003 (卸妆油) |
| NULL |
+
9 rows in set (0.00 sec)
mysql> select Concat(id,' (',LTrim(name),')') from product;
+
| Concat(id,' (',LTrim(name),')') |
+
| c001 (防晒霜) |
| c002 (护手霜) |
| c003 (面膜) |
| c004 (粉饼) |
| c005 (口红) |
| c006 (水乳) |
| c007 (粉底液 ) |
| c003 (卸妆油) |
| NULL |
+
9 rows in set (0.01 sec)
mysql> select Concat(id,' (',Trim(name),')') from product;
+
| Concat(id,' (',Trim(name),')') |
+
| c001 (防晒霜) |
| c002 (护手霜) |
| c003 (面膜) |
| c004 (粉饼) |
| c005 (口红) |
| c006 (水乳) |
| c007 (粉底液) |
| c003 (卸妆油) |
| NULL |
+
SELECT通常用来从表中检索数据,但可以省略from子句以便简单地访问和处理表达式. 如:
mysql> select 3*2;
+
| 3*2 |
+
| 6 |
+
1 row in set (0.00 sec)
mysql> select Trim(' abc ');
+
| Trim(' abc ') |
+
| abc |
+
1 row in set (0.00 sec)
mysql> select Now();
+
| Now() |
+
| 2021-12-02 22:15:43 |
+
1 row in set (0.01 sec)
|