프로그래머스 Mysql 문제를 풀다가 그룹화된 레코드의 조건을 where절로 걸어주었지만 되지 않았다.
그룹화된 레코드의 조건을 걸기위해선 having을 사용하여한다.
1. WHERE
WHERE 절은 레코드를 필터링할 때 사용되며, 지정한 조건을 만족하는 레코드만 추출하는데 사용된다.
예시
SELECT *
FROM Customers
WHERE Country='Mexico'; // 국가가 Mexico인 조건
SELECT *
FROM Customers
WHERE CustomerID = 1; // 고객ID가 1인 조건
// Tip 글자는 따옴표를 감싸주고, 숫자는 감싸주지 않는다.
연습해보기
https://www.w3schools.com/mysql/mysql_where.asp
MySQL WHERE Clause
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
2. HAVING
HAVING 절은 GROUP BY 절과 함께 SELECT 문에서 표시되는 그룹화된 레코드를 지정한다. GROUP BY로 레코드를 결합한 후에 HAVING을 사용하면 GROUP BY 절에 의해 그룹화된 레코드 중 HAVING 절의 조건을 만족하는 모든 레코드가 표시된다.
예시
SELECT COUNT(CustomerID), Country // 국가와 국가의 고객 수
FROM Customers
GROUP BY Country // 국가를 그룹으로 묶어준다.
HAVING COUNT(CustomerID) >= 5; // 고객이 5명 이상인 조건
연습해보기
https://www.w3schools.com/sql/sql_having.asp
SQL HAVING Clause
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
댓글