https://www.hackerrank.com/challenges/draw-the-triangle-1
SET @row_num = 20;
SELECT
IF(@row_num := @row_num - 1, REPEAT('* ', @row_num + 1), '*')
FROM information_schema.tables
-- solution 2
WITH RECURSIVE P AS(
SELECT 20 AS n -- 초기값 설정
UNION ALL -- 위 쿼리와 아래 쿼리를 연산
SELECT n-1 FROM P -- 하나씩 불러옴
WHERE n > 0 -- 반복 종료 조건
)
SELECT REPEAT('* ', n) FROM P
-- solution 3
SELECT REPEAT('* ', 21 - (row_number() over()))
FROM information_schema.tables
LIMIT 20
-- RECURSIVE
WITH RECURSIVE cte_name AS (
initial_query -- anchor member
UNION ALL
recursive_query -- recursive member that references to the CTE name
)
SELECT * FROM cte_name
코드 참고(수림님)
https://www.hackerrank.com/challenges/draw-the-triangle-2
SET @row_num = 0;
SELECT
IF(@row_num := @row_num + 1, REPEAT('* ', @row_num), '')
FROM information_schema.tables
LIMIT 20
-- solution 2
WITH RECURSIVE P AS(
SELECT 1 AS n -- 초기값 설정
UNION ALL -- 위 쿼리와 아래 쿼리를 연산
SELECT n + 1 FROM P -- 하나씩 불러옴
WHERE n < 20 -- 반복 종료 조건
)
SELECT REPEAT('* ', n) FROM P