Draw Triangel 1. HackerRank SQL Problem

Problem Description

Analysis:

 We can use REPEAT function to solve problem. The syntax is REPEAT(str, count), str is the string to repeat and count is how many times it will repeat. And we also need a table with at least 20 rows, then we can apply        REPEAT function once for each row. Additionally, we need a variable to represent the length of each line.

 First, we declare a variable called number and set it to 21:

SET @number = 21;
 

 Then, we choose a table with at least 20 rows and we apply REPEAT function for each row. Also, we use the variable number to record the length of each line. As a result, we need to decrease the number by 1 every time        before we apply the REPEAT function:

SET @number = 21;
SELECT REPEAT('* ', @number := @number - 1) FROM information_schema.tables;

We initialize the variable with 21 and decrease it by 1 for each row. But what if the variable becomes less than 0? The REPEAT function will return null. So, for example, we choose a table with 30 rows, the query will output 30 lines, 20 lines as we expect and 10 lines of null. It is better to exclude null lines.

By limiting the number of lines to output:

SET @number = 21;
SELECT REPEAT('* ', @number := @number - 1) FROM information_schema.tables LIMIT 20;

 

  

  

猜你喜欢

转载自www.cnblogs.com/codingyangmao/p/10907293.html
今日推荐