如何使用 MySQL SELECT 向已建立的表中新增列?


我們首先建立一個表 −

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)

更新日期:30-7-2019

237 次瀏覽

開啟您的 職業生涯

完成本課程並獲得認證

開始
廣告
© . All rights reserved.