MS SQL版MySQL重复功能

时间:2020-12-25 16:40:02

I was looking into creating a nested set for a hierarchical structure to create a categories table for a web site on an MSSQL 2008 express database. I have been following this guide which was written for MySQL: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ and was wondering what the MSSQL version of the REPEAT function in the following statement is:

我正在研究为层次结构创建嵌套集合,以便为MSSQL 2008 Express数据库上的网站创建类别表。我一直在关注为MySQL编写的本指南:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/,并想知道以下语句中的REPEAT函数的MSSQL版本是什么:

SELECT CONCAT( REPEAT( ' ', (COUNT(parent.name) - 1) ), node.name) AS name
FROM nested_category AS node,
    nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;

Thanks in advance

提前致谢

2 个解决方案

#1


3  

I believe you're looking for REPLICATE:

我相信你正在寻找REPLICATE:

SELECT REPLICATE(' ', COUNT(parent.name) - 1) + node.name AS name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;

#2


0  

You're looking for TSQL's REPLICATE() function which takes the same parameters in the same order for the same effect.

您正在寻找TSQL的REPLICATE()函数,该函数以相同的顺序采用相同的参数以获得相同的效果。

Edit: Maybe I should clarify, there are some differences in border cases like the count being negative, but for "normal" use, it behaves like the same function.

编辑:也许我应该澄清,在计数为负数的边界情况下存在一些差异,但对于“正常”使用,它的行为类似于相同的功能。

#1


3  

I believe you're looking for REPLICATE:

我相信你正在寻找REPLICATE:

SELECT REPLICATE(' ', COUNT(parent.name) - 1) + node.name AS name
FROM nested_category AS node,
nested_category AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.name
ORDER BY node.lft;

#2


0  

You're looking for TSQL's REPLICATE() function which takes the same parameters in the same order for the same effect.

您正在寻找TSQL的REPLICATE()函数,该函数以相同的顺序采用相同的参数以获得相同的效果。

Edit: Maybe I should clarify, there are some differences in border cases like the count being negative, but for "normal" use, it behaves like the same function.

编辑:也许我应该澄清,在计数为负数的边界情况下存在一些差异,但对于“正常”使用,它的行为类似于相同的功能。