在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)
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短