먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable1452 -> ( -> FavouriteColor varchar(50) -> ); Query OK, 0 rows affected (2.42 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable1452 values('Red'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1452 values('Yellow'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1452 values('Blue'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable1452 values('Blue'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable1452 values('Red'); Query OK, 1 row affected (0.29 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select * from DemoTable1452;
이것은 다음과 같은 출력을 생성합니다 -
+----------------+ | FavouriteColor | +----------------+ | Red | | Yellow | | Yellow | | Yellow | | Blue | | Blue | | Red | +----------------+ 7 rows in set (0.00 sec)
다음은 여러 행을 계산하고 다른 열에 결과를 표시하는 쿼리입니다 -
mysql> select sum(FavouriteColor='Red') as NumberOfRedColor, -> sum(FavouriteColor='Yellow') as NumberOfYellowColor, -> sum(FavouriteColor='Blue') as NumberOfBlueColor -> from DemoTable1452;
이것은 다음과 같은 출력을 생성합니다 -
+------------------+---------------------+-------------------+ | NumberOfRedColor | NumberOfYellowColor | NumberOfBlueColor | +------------------+---------------------+-------------------+ | 2 | 3 | 2 | +------------------+---------------------+-------------------+ 1 row in set (0.27 sec)