WHERE절과 별명

GOYENA·2025년 3월 27일

algorithm

목록 보기
9/12

WHERE절과 별명

✏️ WHERE절에서 substr(p.time, 1, 2)의 별명 hh를 인식 못 함

AS로 컬럼의 별명을 부여
부여된 별명은 GROUP BY절 이후 적용
∴GROUP BY 전보다 먼저 실행되는 WHERE절에선 별명 사용 불가능

실행순서
FROM - WHERE - GROUP BY - HAVING - SELECT - ORDER BY

❔오류난 코드

select f.restaurant_name,
	   substr(p.time, 1, 2) hh,
	   count(1) cnt_order
from food_orders f inner join payments p on f.order_id=p.order_id 
WHERE hh BETWEEN 15 and 20
GROUP by 1, 2

❕해결된 코드

select f.restaurant_name,
	   substr(p.time, 1, 2) hh,
	   count(1) cnt_order
from food_orders f inner join payments p on f.order_id=p.order_id 
WHERE substr(p.time, 1, 2) BETWEEN 15 and 20	
GROUP by 1, 2

별칭이 아닌 컬럼명 그대로 사용.

profile
헤헷

0개의 댓글