在 MySQL 中通过值添加临时列?
借助以下语法,你可以使用值添加临时列 −
select yourColumnName1,yourColumnName2,.....N ,yourTemporaryColumnValue as yourTemporaryColumnName from yourTableName;
创建一个表以使用值添加临时列。这是查询 −
mysql> create table TemporaryColumnWithValueDemo −> ( −> StudentId int, −> StudentName varchar(100) −> ); Query OK, 0 rows affected (0.59 sec)
在表格中插入一些记录。插入记录的查询如下 −
mysql> insert into TemporaryColumnWithValueDemo values(101,'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into TemporaryColumnWithValueDemo values(102,'Johnson'); Query OK, 1 row affected (0.15 sec) mysql> insert into TemporaryColumnWithValueDemo values(103,'Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into TemporaryColumnWithValueDemo values(104,'Sam'); Query OK, 1 row affected (0.16 sec)
显示所有上面插入的记录。显示所有记录的查询如下 −
mysql> select *from TemporaryColumnWithValueDemo;
以下是输出 −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 101 | John | | 102 | Johnson | | 103 | Carol | | 104 | Sam | +-----------+-------------+ 4 rows in set (0.00 sec)
以下是使用临时值添加列的查询。查询如下 −
mysql> select StudentId,StudentName,'M.I.T.' as TempCollegeName from TemporaryColumnWithValueDemo;
以下是输出。临时列已成功添加 −
+-----------+-------------+-----------------+ | StudentId | StudentName | TempCollegeName | +-----------+-------------+-----------------+ | 101 | John | M.I.T. | | 102 | Johnson | M.I.T. | | 103 | Carol | M.I.T. | | 104 | Sam | M.I.T. | +-----------+-------------+-----------------+ 4 rows in set (0.00 sec)
广告