Home >Database >Mysql Tutorial >How to print out different triangle shapes using SQL? (example)

How to print out different triangle shapes using SQL? (example)

藏色散人
藏色散人Original
2019-03-20 14:53:475212browse

How to print out different triangle shapes using SQL? (example)

Let us see how to print various types of triangle shapes using SQL. (Related recommendations: "MySQL Tutorial" "SQL Tutorial")

Grammar:

Declare @variable_name DATATYPE     -- 首先用数据类型(int)声明所有变量
                                   
select @variable = WITH_ANY_VALUE   -- 选择变量并用值初始化

while CONDITION                     -- 例如@variable > 0

begin                               -- 开始

print replicate('*', @variable)     -- 在变量times中复制插入*字符

set increment/decrement             -- 递增/递减
                                    -- @variable= @variable+1
END                                 -- while循环结束

One:

DECLARE @var int               -- 声明
SELECT @var = 5                -- 初始化
WHILE @var > 0                 -- 条件
BEGIN                          -- 开始
PRINT replicate('* ', @var)    -- 打印
SET @var = @var - 1            -- 递减
END                            -- END

Output:

* * * * *
* * * * 
* * * 
* * 
*

The second type:

DECLARE @var int                  -- 声明
SELECT @var = 1                   -- 初始化
WHILE @var <= 5                   -- 条件
BEGIN                             -- 开始
PRINT replicate(&#39;* &#39;, @var)       -- Print 
SET @var = @var + 1               -- Set 
END                               -- end

Output:

*
* *
* * *
* * * *
* * * * *

Third type:

DECLARE @var int, @x int                 -- 声明两个变量
SELECT @var = 4,@x = 1                   -- 初始化
WHILE @x <=5                             -- 条件
BEGIN
PRINT space(@var) + replicate(&#39;*&#39;, @x)   -- here space for  
                                         -- create spaces  
SET @var = @var - 1                      -- set 
set @x = @x + 1                          -- set 
END                                      -- End

Output:

    *
   **
  ***
 ****
*****

Fourth type:

DECLARE @var int, @x int                 -- 声明两个变量
SELECT @var = 0,@x = 5                   -- 初始化
WHILE @x > 0                             -- 条件
BEGIN
PRINT space(@var) + replicate(&#39;*&#39;, @x)   -- here space for 
                                         -- create spaces  
SET @var = @var + 1                      -- set 
set @x = @x - 1                          -- set 
END                                      -- End

Output:

*****
 ****
  ***
   **
    *

This article is about using SQL to print out different triangles. It is simple and interesting. I hope it will be helpful to friends in need!

The above is the detailed content of How to print out different triangle shapes using SQL? (example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn