Home > Database > Mysql Tutorial > body text

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

藏色散人
Release: 2019-03-20 14:53:47
Original
5190 people have browsed it

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循环结束
Copy after login

One:

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

Output:

* * * * *
* * * * 
* * * 
* * 
*
Copy after login

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
Copy after login

Output:

*
* *
* * *
* * * *
* * * * *
Copy after login

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
Copy after login

Output:

    *
   **
  ***
 ****
*****
Copy after login

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
Copy after login

Output:

*****
 ****
  ***
   **
    *
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template