2

我正在更新网页搜索表单。用户选择他们想要搜索的组名。在操作页面上,为了简单起见,我最终得到以下查询:

select GroupName
from    groupTable 
where groupName in ('Motley Crue','Alvin and the Chipmunks')

通常,这对我来说不是问题,但在这种情况下,数据库字段还可能包含一串名称,例如:

Big Dog's Chair,Purple Dragon,Just Johnny,Johnny Faster,Van Halen

我相信我将不得不用逗号分隔列表中的每个项目遍历数据库字段中的每个项目。如果是这种情况,我不知道该怎么做,也不知道在 SO 或 google 中使用什么搜索词。我可以使用一些帮助。

数据库:MSSQL 2005 Coldfusion:CF9

我在中部标准时间 5:00 下班,所以如果你回复,我可能要到明天才能看到它。

4

2 回答 2

2

马特在正确的轨道上,但是:

如果组名是组的子字符串,则该解决方案可能会产生意外结果。例如,假设用户选择了“粉红色”。该查询还将匹配“Pink Floyd”或“Big Pink”或任何其他包含 Pink 的乐队。

我假设您的表单变量的名称是“selectedGroups”。将其更改为实际情况,它应该可以工作。

select
    groupName
from
    groupTable
where
    <!--- I thought I knew a more graceful way of doing this but it isn't coming to me --->
    <cfloop list = '#form.selectedGroups#' index = "i">
        groupName = '#i#' <!--- match the group if the column only contains one value --->
    or  groupName like '#i#,%' <!--- match the group if it is the first group in the list --->
    or  groupName like '%,#i#,%' <!--- match the group name if it is in the middle of the list --->
    or  groupName like '%,#i#' <!--- match the group name if it is the last group in the list --->
    #i neq listLast(form.selectedGroups) ? ' or ' : ''# <!--- add an or for the next value to search --->
    </cfloop>
于 2012-10-31T13:16:22.617 回答
1

这不像将它们分成多列那样理想,但你可以这样做

select GroupName
from groupTable 
where (groupName like '%Motley Crue%'
 or groupname like '%Alvin and the Chipmunks%'
)
于 2012-10-30T21:49:07.537 回答