简体   繁体   English

从 SQL 服务器中的列表中获取不同的值

[英]Get Distinct value from a list in SQL Server

I have a DB column that has a comma delimited list:我有一个带有逗号分隔列表的 DB 列:

VALUES          ID
--------------------
1,11,32          A
11,12,28         B
1                C
32,12,1          D

When I run my SQL statement, in my WHERE clause I have tried IN , CONTAINS and LIKE with varying degrees of errors and success, but none offer an exact return of what I need.当我运行我的 SQL 语句时,在我的WHERE子句中,我尝试了INCONTAINSLIKE不同程度的错误和成功,但没有一个提供我需要的确切回报。

What I need is a where clause that if I'm looking for all IDs with vale of '1' (NOT the number) in the list.我需要的是一个 where 子句,如果我要在列表中查找所有值为“1”(不是数字)的 ID。

Example of problem:问题示例:

WHERE values like (1) WHERE值如 (1)

This will return A,B,C,D because 1 is included in the value (11).这将返回 A,B,C,D 因为值 (11) 中包含 1。 I would expect IDs (A,C,D).我希望 ID(A,C,D)。

WHERE values like (2) WHERE值如 (2)

This will return A,B,D because 2 is included in the value (32,28,12).这将返回 A,B,D,因为 2 包含在值 (32,28,12) 中。 I would expect zeros records.我希望零记录。

Thanks in advance for your help!在此先感谢您的帮助!

I will begin my answer by quoting the spot-on comment given by @Jarlh above:我将通过引用上面@Jarlh 给出的现场评论开始我的回答:

Never, ever store data as comma separated items.永远不要将数据存储为逗号分隔的项目。 It will only cause you lots of trouble.只会给你带来很多麻烦。

That being said, if you're really stuck with this design, you could use:话虽这么说,如果你真的坚持这种设计,你可以使用:

SELECT *
FROM yourTable
WHERE ',' + [VALUES] + ',' LIKE '%,1,%';

The trick here is convert every VALUES into something looking like:这里的技巧是将每个VALUES转换为如下所示的内容:

,11,12,28,

Then, we can search for a target number with comma delimiters on both sides.然后,我们可以搜索两边都带有逗号分隔符的目标编号。 Since we placed commas at both ends, then every number in the CSV list is now guaranteed to have commas around it.由于我们在两端放置了逗号,因此现在可以保证 CSV 列表中的每个数字周围都有逗号。

If you are stuck with such a poor data model, I would suggest:如果您遇到如此糟糕的数据 model,我建议:

select t.*
from t
where exists (select 1
              from string_split(t.values, ',') s
              where s.value = 1
             );

Exactly i echo what jarlh and Tim says.正是我回应了 jarlh 和 Tim 所说的。 relational model is not the right place to store comma delimited strings in table.关系 model 不是在表中存储逗号分隔字符串的正确位置。

Here is an approach, that can likely use an index if there is one on column x这是一种方法,如果 x 列上有索引,则可能会使用索引

select distinct x
  from t
 cross apply string_split(t.x,',')
where value=1 /*out here you may parameterize, and also could make use of an index each if there is one in value*/ 

+---------+
|    x    |
+---------+
|       1 |
| 1,11,32 |
| 32,12,1 |
+---------+

working example https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b9b3084f52b0f42ffd17d90427016999工作示例https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=b9b3084f52b0f42ffd17d90427016999

--SQL Server older versions --SQL Server 旧版本

with data
 as (
SELECT t.c.value('.', 'VARCHAR(1000)') as val
      ,y
      ,x
           FROM (
                 SELECT x1 = CAST('<t>' + 
                             REPLACE(x , ',', '</t><t>') + '</t>' AS XML)
                        ,y 
                        ,x
                   FROM t
                ) a
     CROSS APPLY x1.nodes('/t') t(c)
     )
select x,y
  from data

+---------+
|    x    |
+---------+
|       1 |
| 1,11,32 |
| 32,12,1 |
+---------+

working example https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=011a096bbdd759ea5fe3aa74b08bc895工作示例https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=011a096bbdd759ea5fe3aa74b08bc895

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

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