Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用mySQL递归CTE生成一个三角形的星星_Mysql_Sql - Fatal编程技术网

使用mySQL递归CTE生成一个三角形的星星

使用mySQL递归CTE生成一个三角形的星星,mysql,sql,Mysql,Sql,我正在尝试在MYSQL中生成一个三角形的星星,如下所示: ***** **** *** ** * 我在MYSQL中使用以下代码 with recursive print_star(n) as ( select '*' UNION ALL select concat(n,'*') from print_star where length(n)<5 ) select * from print_star order by length(n) desc

我正在尝试在MYSQL中生成一个三角形的星星,如下所示:

*****
****
***
**
*
我在MYSQL中使用以下代码

with recursive print_star(n) as (
    select '*'
    UNION ALL
    select concat(n,'*')
    from print_star
    where length(n)<5
)
select * from print_star order by length(n) desc

对于第1行的“n”列,我获取的错误数据太长。有谁能帮我找出问题所在吗?

我想MySQL对类型很挑剔。试试这个:

with recursive print_star(n) as (
    select cast('*' as char(255)) n
    union all
    select concat(n, '*')
    from print_star
    where length(n) < 5
)
select *
from print_star
order by length(n) desc

在我们将“*”转换为char255之前,知道它是什么类型吗?@nipunPararasrampuria。我猜是char1。SQLServer也有同样的问题,需要注意递归CTE中字符串的长度。你可能认为MySQL的人会想避免一个已知的问题。太棒了!感谢您提供的附加上下文!我非常感激@尼普·帕拉姆布里亚:这是一个瓦查尔。创建表abcd作为选择“*”;描述abcd;