เพิ่มคอลัมน์ที่ไม่มีอยู่ในตารางได้ง่ายด้วยหลาย AS keyword
มาสร้างตาราง:
mysql> create table ColumnDoesNotExists ( UserId int, UserName varchar(20) ); Query OK, 0 rows affected (0.67 sec)
แทรกข้อมูล 9 แถว...
แสดงข้อมูล:
mysql> select * from ColumnDoesNotExists;
ผลลัพธ์
+--------+----------+ | UserId | UserName | +--------+----------+ | 100 | Larry | | 101 | Sam | | 102 | Mike | | 103 | David | | 104 | Robert | | 105 | Maxwell | | 106 | Bob | | 107 | John | | 108 | James | +--------+----------+ 9 rows in set (0.00 sec)
ตัวอย่าง
เพิ่มคอลัมน์ Age (ไม่มีในตาราง):
mysql> select UserId, UserName, 23 AS Age from ColumnDoesNotExists;
ผลลัพธ์มีคอลัมน์ Age เพิ่มมา:
+--------+----------+-----+ | UserId | UserName | Age | +--------+----------+-----+ | 100 | Larry | 23 | | 101 | Sam | 23 | ... | 108 | James | 23 | +--------+----------+-----+ 9 rows in set (0.00 sec)
ตัวอย่าง
เพิ่มหลายคอลัมน์พร้อมกัน (Marks, CountryName):
mysql> select UserId, UserName, 23 AS Age, 99 AS Marks, 'UK' AS CountryName from ColumnDoesNotExists;
ผลลัพธ์:
+--------+----------+-----+-------+-------------+ | UserId | UserName | Age | Marks | CountryName | +--------+----------+-----+-------+-------------+ | 100 | Larry | 23 | 99 | UK | ... | 108 | James | 23 | 99 | UK | +--------+----------+-----+-------+-------------+ 9 rows in set (0.00 sec)