如果用户将搜索键作为字符串输入,则搜索数据

时间:2009-11-24 12:29:29

标签: sql-server-2005

我正在开发一个项目,其中包括一个搜索任务,输入字符串作为搜索键。我制作了一个表格,用于存储用户输入的所有单词。我编写了一个从字符串中分离单词的函数。直到它完美无缺。

我想搜索输入密钥的候选人。

Id - > INT
UserID - > int
字处理> VARCHAR(最大)

以下是一些重新编码器

ID用户ID字
...............................................
1个29 ASP.Net
2 29的Java
3个月29日经理
4月27日软件
5 29开发

实际上,当我获得密钥的搜索字符串时,我将每个单词分开并使用“in”准备查询。

查询

Select distinct(UserID) 
from dbo.Dictionary 
where UserID in (select UserID 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='ASP.NET') 
and UserID in (select UserID 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='SYSTEM') 
and UserID in (select UserID 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='MANAGER') 
and UserID in (select UserID 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='JAVA') 
and UserID in (select UserID 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='ASP.NET') 
and UserID in(select UserID 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='KAMLESH') 
and UserID in(select UserID from 
                 from dbo.Dictionary 
                 where rtrim(ltrim(upper(Word)))='PROJECT')

在某个级别的两个或三个键上它可以很好地工作但是在那之后添加更多的键,比如6个或更多,它会给出错误:

Msg 8623, Level 16, State 1, Line 1

The query processor ran out of internal resources and could not produce a  
query plan. This is a rare event and only expected for extremely complex 
queries or queries that reference a very large number of tables or partitions. 
Please simplify the query. If you believe you have received this message in
error, contact Customer Support Services for more information.

如果用户将搜索键作为字符串输入,我想知道如何搜索数据(CandidateID),而不会对网站的速度产生影响。

3 个答案:

答案 0 :(得分:1)

您应该能够使用IN语句来表示

Select distinct(UserID) 
from dbo.Dictionary 
where UserID in(select UserID  
               from dbo.Dictionary 
               where rtrim(ltrim(upper(Word)))IN  
         ('ASP.NET','SYSTEM','MANAGER','JAVA','ASP.NET','KAMLESH','PROJECT'))

由于请求编辑:

将此示例视为开始。

DECLARE @Table TABLE(
        UserID INT,
        Words VARCHAR(10)
)

INSERT INTO @Table (UserID, Words) SELECT 1, 'A'
INSERT INTO @Table (UserID, Words) SELECT 2, 'B'
INSERT INTO @Table (UserID, Words) SELECT 3, 'C'
INSERT INTO @Table (UserID, Words) SELECT 3, 'C'
INSERT INTO @Table (UserID, Words) SELECT 1, 'B'
INSERT INTO @Table (UserID, Words) SELECT 1, 'C'

DECLARE @Keys TABLE(
        KeyVal VARCHAR(10)
)

INSERT INTO @Keys SELECT 'B'
INSERT INTO @Keys SELECT 'C'

SELECT  UserID
FROM    (
            SELECT  DISTINCT 
                    UserID,
                    Words
            FROM    @Table 
        ) DistinctVals INNER JOIN
        @Keys k ON DistinctVals.Words = k.KeyVal
GROUP BY UserID
HAVING COUNT(Words) = (SELECT COUNT(KeyVal) FROM @Keys)

答案 1 :(得分:0)

SELECT (UserID) FROM 
 (SELECT UserID,
        GROUP_CONCAT(DISTINCT RTRIM(LTRIM(UPPER(Word))) ORDER BY Word) AS w                
   FROM dbo.Dictionary
   GROUP BY UserID) AS tmptable
WHERE w= '$querystring';

语法可能需要调整,因为我已经习惯了MySQL,但想法是你的$ querystring包含你需要用逗号分隔的所有术语。

答案 2 :(得分:0)

你可以试试这个:

DECLARE @Table TABLE
( 
        UserID INT, 
        Words VARCHAR(10) 
) 


INSERT INTO @Table (UserID, Words) SELECT 1, 'A' 
INSERT INTO @Table (UserID, Words) SELECT 2, 'C' 
INSERT INTO @Table (UserID, Words) SELECT 3, 'C' 
INSERT INTO @Table (UserID, Words) SELECT 2, 'A' 
INSERT INTO @Table (UserID, Words) SELECT 3, 'B' 
INSERT INTO @Table (UserID, Words) SELECT 1, 'C' 

SELECT T.UserID
  FROM (SELECT UserID, count(distinct Words) catCount
          FROM @Table
         WHERE Words IN ('A','C')
      GROUP BY UserID) T
 WHERE T.catCount = 2 --this number = the number of words passed to the IN operator.