먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable610 (SubjectName varchar(100)); Query OK, 0 rows affected (0.63 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable610 values('MySQL'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable610 values('Java'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable610 values('MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable610 values('MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable610 values('MySQL'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable610 values('MySQL'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable610 values('Java'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable610 values('MongoDB'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable610 values('C'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable610 values('C++'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable610 values('MongoDB'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable610 values('MySQL'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable610 values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable610 values('Python'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable610 values('MySQL'); Query OK, 1 row affected (0.15 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable610;
이것은 다음과 같은 출력을 생성합니다 -
+-------------+ | SubjectName | +-------------+ | MySQL | | Java | | MySQL | | MongoDB | | MySQL | | MySQL | | Java | | MongoDB | | C | | C++ | | MongoDB | | MySQL | | Java | | Python | | MySQL | +-------------+ 15 rows in set (0.00 sec)
다음은 가장 많이 발생하는 항목만 선택하는 쿼리입니다. -
mysql> select SubjectName from ( select SubjectName from DemoTable610 group by SubjectName order by count(SubjectName) DESC LIMIT 2 ) AS tbl order by SubjectName;
이것은 다음과 같은 출력을 생성합니다 -
+-------------+ | SubjectName | +-------------+ | Java | | MySQL | +-------------+ 2 rows in set (0.04 sec)