简体   繁体   English

SQL Server 2014相当于mysql的find_in_set()

[英]SQL Server 2014 equivalent to mysql's find_in_set()

I'm working with a database that has a locations table such as: 我正在使用具有位置表的数据库,例如:

locationID | locationHierarchy
1          | 0
2          | 1
3          | 1,2
4          | 1
5          | 1,4
6          | 1,4,5

which makes a tree like this 像这样的树

1
--2
----3
--4
----5
------6

where locationHierarchy is a csv string of the locationIDs of all its ancesters (think of a hierarchy tree). 其中locationHierarchy是其所有祖先的locationID的csv字符串(以层次结构树为例)。 This makes it easy to determine the hierarchy when working toward the top of the tree given a starting locationID. 在给定起始locationID的情况下,在朝树的顶部工作时,可以轻松确定层次结构。

Now I need to write code to start with an ancestor and recursively find all descendants. 现在,我需要编写代码以始祖,并递归地找到所有后代。 MySQL has a function called 'find_in_set' which easily parses a csv string to look for a value. MySQL具有一个名为“ find_in_set”的函数,该函数可以轻松解析csv字符串以查找值。 It's nice because I can just say "find in set the value 4" which would give all locations that are descendants of locationID of 4 (including 4 itself). 很好,因为我可以说“找到设置值4”,这将给出locationID为4的后代(包括4本身)的所有位置。

Unfortunately this is being developed on SQL Server 2014 and it has no such function. 不幸的是,这是在SQL Server 2014上开发的,没有任何功能。 The CSV string is a variable length (virtually unlimited levels allowed) and I need a way to find all ancestors of a location. CSV字符串的长度是可变的(几乎允许无限制的级别),我需要一种方法来查找某个位置的所有祖先。

A lot of what I've found on the internet to mimic the find_in_set function into SQL Server assumes a fixed depth of hierarchy such as 4 levels maximum) which wouldn't work for me. 我在互联网上发现的许多将find_in_set函数模仿到SQL Server的功能都假定了固定的层次结构深度(例如最多4个级别),这对我不起作用。

Does anyone have a stored procedure or anything that I could integrate into a query? 是否有人有存储过程或我可以集成到查询中的任何内容? I'd really rather not have to pull all records from this table to use code to individually parse the CSV string. 我真的希望不必从该表中提取所有记录来使用代码分别解析CSV字符串。

I would imagine searching the locationHierarchy string for locationID% or %,{locationid},% would work but be pretty slow. 我可以想象在locationHierarchy字符串中搜索locationID%或%,{locationid},%会起作用,但是速度很慢。

I think you want like -- in either database. 我想你想要like -任一数据库。 Something like this: 像这样:

select l.*
from locations l
where l.locationHierarchy like @LocationHierarchy + ',%';

If you want the original location included, then one method is: 如果要包括原始位置,则一种方法是:

select l.*
from locations l
where l.locationHierarchy + ',' like @LocationHierarchy + ',%';

I should also note that SQL Server has proper support for recursive queries, so it has other options for hierarchies apart from hierarchy trees (which are still a very reasonable solution). 我还应注意,SQL Server对递归查询提供了适当的支持,因此它除了层次结构树以外,还为层次结构提供了其他选择(这仍然是一个非常合理的解决方案)。

Finally It worked for me.. 终于对我有用。

SELECT * FROM locations WHERE locationHierarchy like CONCAT(@param,',%%')    OR
                                    o.unitnumber like CONCAT('%%,',@param,',%%') OR
                                    o.unitnumber like CONCAT('%%,',@param)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM