几乎可以在SQL中创建如下所示的星形三角形。我知道这可以在任何其他编程语言(如C,C ++,Java)中轻松完成,但想知道它是否真的可以用于SQL或PL / SQL。我尝试使用Oracle中的双表来处理它,但无法通过它。
* *
* * * *
* * * or * * *
如果有人知道的话,有人可以请一些。
答案 0 :(得分:8)
最简单的方法就是这样。如果你想建立等边三角形而不是直角三角形,你可以变得更复杂。
SQL> ed
Wrote file afiedt.buf
1 select rpad( '* ', level*2, '* ' )
2 from dual
3* connect by level <= 3
SQL> /
RPAD('*',LEVEL*2,'*')
--------------------------------------------------------------------------------
*
* *
* * *
答案 1 :(得分:2)
不确定您要找的是什么。也许这个?
select '*' from dual
union all select '**' from dual
union all select '***' from dual
答案 2 :(得分:1)
试试这个..
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
答案 3 :(得分:0)
如果您想要的只是简单的三角形,那么您可以这样做:
SELECT '*' FROM table
UNION
SELECT '**' FROM table
UNION
SELECT '***' FROM table
答案 4 :(得分:0)
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
答案 5 :(得分:0)
[等式训练器]我们可以使用Oracle SQL进行金字塔构造,如下所示。
myTheme <- chart_theme()
myTheme$col$`bg` <- "#000000"
**这里的5是行数。
让我们扭转它,
select rpad(' ',5 -level) || rpad( '* ', level*2, '* ' )
from dual
connect by level <= 5;
答案 6 :(得分:-1)
这是在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
*
***
*****
*******
*********
你可以在这个链接上获得更多的脚本和帮助......这对我有帮助
链接: - sqlquerynscript
答案 7 :(得分:-1)
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
***** **** *** ** *
答案 8 :(得分:-1)
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
答案 9 :(得分:-2)
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)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *