如何使用 MySQL SELECT 在已创建的表中添加列?

mysqlmysqli database更新于 2025/9/13 18:20:17

首先我们创建一个表 −

mysql> create table DemoTable
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(100),
   -> Age int
   -> );
Query OK, 0 rows affected (0.49 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable(Name,Age) values('Robert',24);
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(Name,Age) values('Chris',22);
Query OK, 1 row affected (0.13 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

+----+--------+------+
| Id | Name   | Age  |
+----+--------+------+
| 1  | Robert | 24   |
| 2  | Chris  | 22   |
+----+--------+------+
2 rows in set (0.00 sec)

这是使用 SELECT 添加新列的查询 −

mysql> select Id,Name,Age,'US' AS DefaultCountryName from DemoTable;

输出

+----+--------+------+--------------------+
| Id | Name   | Age  | DefaultCountryName |
+----+--------+------+--------------------+
| 1  | Robert | 24   | US                 |
| 2  | Chris  | 22   | US                 |
+----+--------+------+--------------------+
2 rows in set (0.00 sec)

相关文章


有用资源