서브쿼리란? SQL 명령문 안에 지정하는 하부 SELECT 명령으로 부수적인 질의를 말한다.
mysql> SELECT * FROM sample54;
+------+------+
| no | a |
+------+------+
| 1 | 100 |
| 2 | 900 |
| 3 | 20 |
| 4 | 80 |
+------+------+
4 rows in set (0.01 sec)
# a 열의 최솟값 검색
mysql> SELECT MIN(a) FROM sample54;
+--------+
| MIN(a) |
+--------+
| 20 |
+--------+
1 row in set (0.00 sec)
# 괄호로 서브쿼리를 지정하여 삭제
mysql> DELETE FROM sample54 WHERE a = (SELECT a FROM(SELECT MIN(a) AS a FROM sample54) AS x);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM sample54;
+------+------+
| no | a |
+------+------+
| 1 | 100 |
| 2 | 900 |
| 4 | 80 |
+------+------+
3 rows in set (0.01 sec)
mysql> set @a = (SELECT MIN(a) FROM sample54);
Query OK, 0 rows affected (0.00 sec)
mysql> DELETE FROM sample54 WHERE a = @a;
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM sample54;
+------+------+
| no | a |
+------+------+
| 1 | 100 |
| 2 | 900 |
+------+------+
2 rows in set (0.00 sec)
# 일반적인 서브쿼리 패턴
1. 하나의 값을 반환하는 패턴 👉 '단일 값'을 '스칼라 값' 이라고 한다.
mysql> SELECT MIN(a) FROM sample54;
+--------+
| MIN(a) |
+--------+
| 100 |
+--------+
1 row in set (0.00 sec)
2. 복수의 행이 반환되지만 열은 하나인 패턴
mysql> SELECT no FROM sample54;
+------+
| no |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
3. 하나의 행이 반환되지만 열이 복수인 패턴
mysql> SELECT MIN(a), MAX(no) FROM sample54;
+--------+---------+
| MIN(a) | MAX(no) |
+--------+---------+
| 100 | 2 |
+--------+---------+
1 row in set (0.00 sec)
4. 복수의 행, 복수의 열이 반환되는 패턴
mysql> SELECT no, a FROM sample54;
+------+------+
| no | a |
+------+------+
| 1 | 100 |
| 2 | 900 |
+------+------+
2 rows in set (0.00 sec)
mysql> SELECT
-> (SELECT COUNT(*) FROM sample51) AS sq1,
-> (SELECT COUNT(*) FROM sample54) AS sq2;
+------+------+
| sq1 | sq2 |
+------+------+
| 5 | 2 |
+------+------+
1 row in set (0.01 sec)
mysql> UPDATE sample54 SET a = (SELECT a FROM(SELECT MAX(a) AS a FROM sample54) AS x);
Query OK, 1 row affected (0.00 sec)
Rows matched: 2 Changed: 1 Warnings: 0
mysql> SELECT * FROM sample54;
+------+------+
| no | a |
+------+------+
| 1 | 900 |
| 2 | 900 |
+------+------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM (SELECT * FROM sample54) sq;
# AS 키워드 사용
mysql> SELECT * FROM (SELECT * FROM sample54) AS sq;
+------+------+
| no | a |
+------+------+
| 1 | 900 |
| 2 | 900 |
+------+------+
2 rows in set (0.00 sec)
# 3단계 중첩구조 예시
mysql> SELECT * FROM (SELECT * FROM (SELECT * FROM sample54) sq1) sq2;
+------+------+
| no | a |
+------+------+
| 1 | 900 |
| 2 | 900 |
+------+------+
2 rows in set (0.00 sec)
mysql> INSERT INTO sample541 VALUES (
-> (SELECT COUNT(*) FROM sample51),
-> (SELECT COUNT(*) FROM sample54)
-> );
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM sample541;
+------+------+
| a | b |
+------+------+
| 5 | 2 |
+------+------+
1 row in set (0.00 sec)
mysql> SELECT * FROM sample541;
+------+------+
| a | b |
+------+------+
| 5 | 2 |
+------+------+
1 row in set (0.00 sec)
mysql> INSERT INTO sample541 SELECT 1,2;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM sample541;
+------+------+
| a | b |
+------+------+
| 5 | 2 |
| 1 | 2 |
+------+------+
2 rows in set (0.00 sec)
! but
SELECT가 반환하는 열 수와 자료형은 일치해야 한다.