Categories : SQL
SQL 예시 - (3) 특정 조건에 맞는 데이터 COUNT (feat. LIKE)
SQL LIKE 예시
0. SQL 쿼리
SELECT columns_type, COUNT(*) AS example
FROM table
WHERE columns LIKE '%A%'
OR columns LIKE '%B%'
OR columns LIKE '%C%'
GROUP BY columns_type
ORDER BY columns_type ASC;
Result :
columns_type | example |
---|---|
A | 10 |
B | 5 |
C | 8 |
1. 쿼리 구성요소
WHERE columns LIKE '%word%' ~
- ‘columns’ 열에 ‘%word%’가 포함되어있는 조건.
SELECT columns_type, COUNT(*) AS example FROM table
- 조건을 만족하는 행의 개수 별칭을 ‘example’ 로 지정.
- 데이터를 가져올 테이블의 이름 ‘table’.
GROUP BY columns_type
- ‘SELECT columns_type, COUNT(*)’을 사용할 경우, ‘GROUP BY columns_type’ 필요.
ORDER BY columns_type ASC
- 데이터를 ‘columns_type’를 오름차순 기준으로 지정.
- 데이터를 ‘columns_type’를 오름차순 기준으로 지정.