如何使用 SQL 打印星形三角形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9930364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print a triangle of stars using SQL
提问by Teja
Is it practically possible to create a triangle of stars like this as below in SQL.I know that this could be done easily in any other programming language like C,C++,Java but want to know whether it is really possible with just SQL or PL/SQL.I tried working on it with dual table in Oracle but couldn't get through it.
实际上是否可以在 SQL 中创建如下所示的三角形三角形。我知道这可以在任何其他编程语言(如 C、C++、Java)中轻松完成,但想知道是否真的可以仅使用 SQL 或 PL /SQL。我尝试在 Oracle 中使用双表处理它,但无法通过它。
* *
* * * *
* * * or * * *
Can someone please shed somelight if anyone knows about it.
如果有人知道的话,有人可以透露一些信息。
回答by Justin Cave
The simplest approach would be something like this. You can get more sophisticated particularly if you want to build the equilateral triangle rather than the right triangle.
最简单的方法是这样的。如果您想构建等边三角形而不是直角三角形,则可以变得更加复杂。
SQL> ed
Wrote file afiedt.buf
1 select rpad( '* ', level*2, '* ' )
2 from dual
3* connect by level <= 3
SQL> /
RPAD('*',LEVEL*2,'*')
--------------------------------------------------------------------------------
*
* *
* * *
回答by RedFilter
回答by raja
Here is the script to Get a perfect triangle or Pyramid in sql (tested in Microsoft Sql 2008)
这是在sql中获取完美三角形或金字塔的脚本(在Microsoft Sql 2008中测试)
declare @x int,@y int
select @x=5,@y=0
while @x>0
begin
print space(@x)+replicate('*',@y)+replicate('*',@y+1)
set @y=@y+1
set @x=@x-1
end
*
***
*****
*******
*********
you can get many more scripts and help at this link... it was helpful to me
你可以在这个链接上获得更多的脚本和帮助......这对我很有帮助
Link:- sqlquerynscript
链接:- sqlquerynscript
回答by Benson
Try this..
尝试这个..
declare @x int,@y int,@diff int
select @x=0,@y=10,@diff=2--diferrence between consecutive rows
while @x<@y
begin
if @x=0 and @diff<>1
print space((@y-@x)*@diff-1)+replicate('*',1)
else if(@diff%2=0)
print space((@y-@x)*@diff)+replicate('* ',@x+(@x*(@diff-1)))
else
print space((@y-@x)*@diff)+replicate('* ',@x+(@x*(@diff-1)))
select @x=@x+1
end
回答by Anoop
declare @count int,@num int,@num1 int, @space int, @str varchar(50)
set @count = 5 set @num = 1
while(@num<=@count)
begin
set @num1 = 0 set @space = @count-@num
while (@num1<@num)
begin
if @str is null
set @str = '* '
else
set @str = @str+'* ' set @num1 = @num1+1
end
print (space(@space)+@str)
set @num = @num+1 set @str = null
end
回答by Justin Pihony
If all you want is the simple triangle, then you can do this:
如果你想要的只是简单的三角形,那么你可以这样做:
SELECT '*' FROM table
UNION
SELECT '**' FROM table
UNION
SELECT '***' FROM table
回答by Sameer Pradhan
[Equilateral Traingle] We can make a pyramid with Oracle SQL as follows.
【等边三角形】我们可以用Oracle SQL做一个金字塔如下。
select rpad(' ',5 -level) || rpad( '* ', level*2, '* ' )
from dual
connect by level <= 5;
** Here 5 is the number of lines.
** 这里 5 是行数。
Let us reverse it,
让我们反过来,
select rpad(' ',level) || rpad( '* ', 2*(5-level+1), '* ' )
from dual
connect by level <= 5;
回答by helpMeLearn
declare @row int = 5,
@index int = 0,
@string nvarchar(5) =''
while @row > 0
begin
set @index = @row
while @index > 0
begin
set @string = '*' + @string
set @index = @index - 1
end
print @string
set @string = ''
set @row = @row - 1
end
***** **** *** ** *
***** **** *** ** *
回答by spandan
DECLARE @lclMaxLevel INT=5
DECLARE @lclPrintCount INT =0
WHILE @lclMaxLevel > 0
BEGIN
PRINT Space(@lclMaxLevel)
+ Replicate('*', @lclPrintCount+1)
SET @lclMaxLevel=@lclMaxLevel - 1
SET @lclPrintCount=@lclPrintCount + 1
END
回答by Rajesh Dutta
select rpad('* ', level * 2, '* ')
from dual connect by
level <= 10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
select rpad(' ',r*2,' ')||rpad('* ',l*2,'* ') k
from ( select level l,row_number() over(order by null) r
from dual
connect by level<=10
order by l desc)
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
select rpad(' ',l*2,' ')||rpad('* ',r*2,'* ') k
from ( select level l,row_number() over(order by null) r
from dual
connect by level<=10
order by l desc)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
select rpad(' ',l,' ')||rpad('* ',r*2,'* ') k
from ( select level l,row_number() over(order by null) r
from dual
connect by level<=10
order by l desc)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *