먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable704 (SubjectName text); Query OK, 0 rows affected (0.58 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable704 values('Introduction to MySQL'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable704 values('Introduction to MongoDB'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable704 values('Introduction to MySQL'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable704 values('Introduction to Java'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable704 values('Introduction to MongoDB'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable704 values('Introduction to MySQL'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable704 values('Introduction to C'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable704 values('Introduction to Spring and Hibernate'); Query OK, 1 row affected (0.12 sec)
select 문을 사용하여 테이블의 모든 레코드 표시 -
mysql> select *from DemoTable704;
그러면 다음과 같은 출력이 생성됩니다. -
+--------------------------------------+ | SubjectName | +--------------------------------------+ | Introduction to MySQL | | Introduction to MongoDB | | Introduction to MySQL | | Introduction to Java | | Introduction to MongoDB | | Introduction to MySQL | | Introduction to C | | Introduction to Spring and Hibernate | +--------------------------------------+ 8 rows in set (0.00 sec)
다음은 열에 문자열이 몇 번 나타나는지 계산하는 쿼리입니다 -
mysql> SELECT COUNT(*) from DemoTable704 WHERE SubjectName LIKE '%Introduction to MySQL%';
그러면 다음과 같은 출력이 생성됩니다. -
+----------+ | COUNT(*) | +----------+ | 3 | +----------+ 1 row in set (0.00 sec)