SQL语句。与In操作数一样使用

时间:2011-12-26 10:08:17

标签: sql sql-server

我正在使用sql server 2005。 我正在尝试搜索名字很少的人。 像这样的东西:

select PersonID from Person 
where Name like + '%' (select [value] dbo.fnSplit('David;John;Kevin', ';')) + '%'

但这不起作用。

5 个答案:

答案 0 :(得分:4)

select PersonID from Person p inner join
(select value as AName from dbo.fnSplit('David;John;Kevin', ';')) Names
on p.Name LIKE '%' + Names.Value + '%'

这应该允许您按照指定使用LIKE。

答案 1 :(得分:4)

您不能将like用于多种模式,但您可以将选择更改为一次使用一种模式:

select PersonID
from Person p
where exists ( select 1 from (
        select [value] as name
        from dbo.fnSplit('David;John;Kevin', ';')
    ) n
    where p.Name like '%'+n.name+'%'
)

答案 2 :(得分:1)

如果您只想分割名称并按名称进行比较,请尝试:

SELECT PersonID FROM Person 
WHERE Name IN (select [value] FROM dbo.fnSplit('David;John;Kevin', ';'))

答案 3 :(得分:0)

试试这个:

select PersonID from Person where Name like + '%David%'
union
select PersonID from Person where Name like + '%John%'
union
select PersonID from Person where Name like + '%Kevin%';

答案 4 :(得分:0)

你可以这样做,你为什么使用fnSplit:

select PersonID from Person where Name like '%David%'or Name like + '%John%'
or Name like + '%Kevin%'