要将列添加到MySQL查询并为其提供值,请使用以下语法-
select yourColumnName1,yourColumnName2,.....N ,yourValue AS anyAliasName from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FirstName) values('John'); mysql> insert into DemoTable(FirstName) values('Larry'); mysql> insert into DemoTable(FirstName) values('Chris'); mysql> insert into DemoTable(FirstName) values('Robert');
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Larry | | 3 | Chris | | 4 | Robert | +----+-----------+ 4 rows in set (0.00 sec)
以下是向MySQL添加列并为其提供值的查询。在这里,我们在创建新列AGE之后将值设置为23-
mysql> select Id,FirstName,23 AS AGE from DemoTable;
这将产生以下输出-
+----+-----------+-----+ | Id | FirstName | AGE | +----+-----------+-----+ | 1 | John | 23 | | 2 | Larry | 23 | | 3 | Chris | 23 | | 4 | Robert | 23 | +----+-----------+-----+ 4 rows in set (0.00 sec)