먼저 데모 테이블을 생성하겠습니다 -
mysql> create table selectPerson -> ( -> PersonId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> PersonName varchar(20), -> PersonFavouriteFruit varchar(60) -> ); Query OK, 0 rows affected (0.58 sec)
삽입 명령을 사용하여 테이블에 일부 레코드를 삽입하십시오. 쿼리는 다음과 같습니다 -
mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('John','Banana'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('John','Blackberry'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('John','Blueberry'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Carol','Apple'); Query OK, 1 row affected (0.19 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Carol','Avocado'); Query OK, 1 row affected (0.17 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Carol','Banana'); Query OK, 1 row affected (0.20 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Sam','Ackee'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Sam','Apple'); Query OK, 1 row affected (0.89 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Sam','Apricots'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectPerson(PersonName,PersonFavouriteFruit) values('Sam','Banana'); Query OK, 1 row affected (0.27 sec)
select 문을 사용하여 테이블의 모든 레코드를 표시합니다. 쿼리는 다음과 같습니다 -
mysql> select *from selectPerson;
다음은 출력입니다 -
+----------+------------+----------------------+ | PersonId | PersonName | PersonFavouriteFruit | +----------+------------+----------------------+ | 1 | John | Banana | | 2 | John | Blackberry | | 3 | John | Blueberry | | 4 | Carol | Apple | | 5 | Carol | Avocado | | 6 | Carol | Banana | | 7 | Sam | Ackee | | 8 | Sam | Apple | | 9 | Sam | Apricots | | 10 | Sam | Banana | +----------+------------+----------------------+ 10 rows in set (0.00 sec)
다음은 Apple과 Banana를 모두 좋아하는 사람을 선택하는 쿼리입니다. -
mysql> SELECT tbl1.PersonName -> FROM selectPerson tbl1 JOIN selectPerson tbl2 on tbl1.PersonName =tbl2.PersonName -> WHERE -> tbl1.PersonFavouriteFruit='Banana' -> and -> tbl2.PersonFavouriteFruit='Apple';
다음은 출력입니다 -
+------------+ | PersonName | +------------+ | Carol | | Sam | +------------+ 2 rows in set (0.00 sec)