Draw Triangel 1. HackerRank SQL Problem

博客介绍了使用REPEAT函数解决问题的方法。需一个至少20行的表,对每行应用该函数,其语法为REPEAT(str, count)。还需一个变量记录每行长度,每次应用函数前将变量减1。同时提到要排除变量小于0时REPEAT函数返回的null行。

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;

 

 

 

  

 

 

  

转载于:https://www.cnblogs.com/codingyangmao/p/10907293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值