Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

MySQL SELECT에서 필드를 생성하는 방법은 무엇입니까?

<시간/>

이를 위해 키워드 AS를 사용하십시오. 먼저 테이블을 생성하겠습니다 -

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

삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -

mysql> insert into DemoTable(Name) values('John');
Query OK, 1 row affected (0.50 sec)
mysql> insert into DemoTable(Name) values('Robert');
Query OK, 1 row affected (0.29 sec)
mysql> insert into DemoTable(Name) values('David');
Query OK, 1 row affected (0.54 sec)

select 문을 사용하여 테이블의 모든 레코드 표시 -

mysql> select *from DemoTable;

출력

+----+--------+
| Id | Name   |
+----+--------+
| 1  | John   |
| 2  | Robert |
| 3  | David  |
+----+--------+
3 rows in set (0.00 sec)

다음은 MySQL SELECT −

에서 필드를 생성하는 쿼리입니다.
mysql> select Id,Name,'US' AS CountryName from DemoTable;

출력

+----+--------+-------------+
| Id | Name   | CountryName |
+----+--------+-------------+
| 1  | John   | US          |
| 2  | Robert | US          |
| 3  | David  | US          | 
+----+--------+-------------+
3 rows in set (0.00 sec)