我该如何做这种选择:
SELECT *
FROM Street
WHERE StreetName LIKE IN ('% Main Street', 'foo %')
请不要告诉我,我可以使用OR
,因为这些实际上来自查询。
答案 0 :(得分:35)
没有合并LIKE
和IN
语法,但您可以LIKE
JOIN
使用;WITH Query(Result) As
(
SELECT '% Main Street' UNION ALL
SELECT 'foo %'
)
SELECT DISTINCT s.*
FROM Street s
JOIN Query q ON StreetName LIKE q.Result
到您的查询中。
SELECT DISTINCT s.*
FROM Street s
JOIN CarStreets cs ON s.StreetName LIKE cs.name + '%'
WHERE cs.Streets = 'offroad'
或者在评论中使用您的示例
{{1}}
答案 1 :(得分:4)
你在这里没有太多选择。
SELECT * FROM Street Where StreetName LIKE '% Main Street' OR StreetName LIKE 'foo %'
如果这是现有的,更复杂的查询(这是我得到的印象)的一部分,您可以创建一个表值函数来为您进行检查。
SELECT * FROM Street Where StreetName IN (dbo.FindStreetNameFunction('% Main Street|foo %'))
我建议使用最简单的解决方案(第一个)。如果这是嵌套在更大,更复杂的查询中,请发布它,我们将看一看。
答案 2 :(得分:1)
You can resort to Dynamic SQL and wrapping up all in a stored procedure.
If you get the LIKE IN param in a string as tokens with a certain separator, like
'% Main Street,foo %,Another%Street'
first you need to create a function that receives a list of LIKE "tokens" and returns a table of them.
CREATE FUNCTION [dbo].[SplitList]
(
@list nvarchar(MAX),
@delim nvarchar(5)
)
RETURNS @splitTable table
(
value nvarchar(50)
)
AS BEGIN
While (Charindex(@delim, @list)>0) Begin
Insert Into @splitTable (value)
Select ltrim(rtrim(Substring(@list, 1, Charindex(@delim, @list)-1)))
Set @list = Substring(@list, Charindex(@delim, @list)+len(@delim), len(@list))
End
Insert Into @splitTable (value) Select ltrim(rtrim(@list))
Return
END
Then in the SP you have the following code
declare
@sql nvarchar(MAX),
@subWhere nvarchar(MAX)
@params nvarchar(MAX)
-- prepare the where sub-clause to cover LIKE IN (...)
-- it will actually generate where sub clause StreetName Like option1 or StreetName Like option2 or ...
set @subWhere = STUFF(
(
--(**)
SELECT ' OR StreetName like ''' + value + '''' FROM SplitList('% Main Street,foo %,Another%Street', ',')
FOR XML PATH('')
), 1, 4, '')
-- create the dynamic SQL
set @sql ='select * from [Street]
where
(' + @subWhere + ')
-- and any additional query params here, if needed, like
AND StreetMinHouseNumber = @minHouseNumber
AND StreetNumberOfHouses between (@minNumberOfHouses and @maxNumberOfHouses)'
set @params = ' @minHouseNumber nvarchar(5),
@minNumberOfHouses int,
@minNumberOfHouses int'
EXECUTE sp_executesql @sql, @params,
@minHouseNumber,
@minNumberOfHouses,
@minNumberOfHouses
Of course, if you have your LIKE IN parameters in another table or you gather it through a query, you can replace that in line (**)
答案 3 :(得分:0)
我相信我可以澄清他在寻找什么,但我不知道答案。我会用我的情况来证明。我有一个表格,其中包含一个名为" Query"持有SQL查询。这些查询有时包含来自我的一个数据库的表名。我需要查找包含特定数据库中的表名的所有Query行。所以,我可以使用以下代码来获取表名:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
我试图使用WHERE IN子句来标识包含我感兴趣的表名的查询行:
SELECT *
FROM [DatasourceQuery]
WHERE Query IN LIKE
(
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
)
我相信OP正试图做那样的事情。
答案 4 :(得分:0)
这是我的方式:
首先创建一个表函数:
create function [splitDelimeter](@str nvarchar(max), @delimeter nvarchar(10)='*')
returns @r table(val nvarchar(max))
as
begin
declare @x nvarchar(max)=@str
set @x='<m>'+replace(@x, @delimeter, '</m><m>')+'</m>'
declare @xx xml=cast(@x as xml)
insert @r(val)
SELECT Tbl.Col.value('.', 'nvarchar(max)') id
FROM @xx.nodes('/m') Tbl(Col)
return
end
然后使用首选项分隔符分割搜索文本。之后,您可以使用左联接进行选择,如下所示:
declare @s nvarchar(max)='% Main Street*foo %'
select a.* from street a
left join gen.splitDelimeter(@s, '*') b
on a.streetname like b.val
where val is not null
答案 5 :(得分:0)
我有一个类似的难题,但是由于只需要匹配字符串的开头,因此将“ like”更改为SUBSTRING:
SELECT *
FROM codes
WHERE SUBSTRING(code, 1, 12) IN ('012316963429', '012315667849')
答案 6 :(得分:0)
我在解决类似问题时所做的是:
SELECT DISTINCT S.*
FROM Street AS S
JOIN (SELECT value FROM String_Split('% Main Street,foo %', N',')) T
ON S.StreetName LIKE T.value;
这在功能上类似于 Martin 的回答,但更直接地回答了问题。
注意:使用 DISTINCT 是因为您可能会在一行中获得多个匹配项。