在MySQL中将字符串连接到SELECT *?
要连接字符串,请使用CONCAT()MySQL中的函数,如以下语法所示
SELECT CONCAT(yourColumnName1,’anyConcatenationString’),CONCAT(yourColumnName2,’anyC oncatenationString’),....N from yourTableName;
为了理解上述语法,让我们首先创建一个表。创建表的查询如下
mysql> create table selectConcat -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> );
使用insert命令在表中插入一些记录。查询如下
mysql> insert into selectConcat values(1,'Carol',23); mysql> insert into selectConcat values(2,'John',24); mysql> insert into selectConcat values(3,'Adam',25); mysql> insert into selectConcat values(4,'Bob',21); mysql> insert into selectConcat values(5,'Sam',22);
使用select语句显示表中的所有记录。查询如下
mysql> select *from selectConcat;
以下是输出
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Carol | 23 | | 2 | John | 24 | | 3 | Adam | 25 | | 4 | Bob | 21 | | 5 | Sam | 22 | +-----------+-------------+------------+ 5 rows in set (0.00 sec)
请与select语句中的字段联系。查询如下
mysql> select concat(StudentId,' as an Id'),concat(StudentName,' as a Name') from selectConcat;
以下是输出
+-------------------------------+----------------------------------+ | concat(StudentId,' as an Id') | concat(StudentName,' as a Name') | +-------------------------------+----------------------------------+ | 1 as an Id | Carol as a Name | | 2 as an Id | John as a Name | | 3 as an Id | Adam as a Name | | 4 as an Id | Bob as a Name | | 5 as an Id | Sam as a Name | +-------------------------------+----------------------------------+ 5 rows in set (0.00 sec)
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练