在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)
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短