如何返回搜索包含输入字符串中所有单词的T-SQL中的记录?

时间:2015-06-11 13:37:00

标签: sql tsql

例如:如果数据库包含

ID  Name           Year  Place of Birth
1   John           1989  Miami
2   Steven         2004  Orlando
3   Cassandra      1990  Chicago
4   John           2000  Orlando 
5   Elizabeth      1989  Houston

搜索John 1989应仅返回第1行。

3 个答案:

答案 0 :(得分:1)

你在谈论WHERE子句吗?

SELECT [ID], [Name], [Year], [Place of Birth]
FROM YourTable
WHERE Name = 'John' AND Year = 1989

或者您想要存储过程?

CREATE PROCEDURE sp_search
@Name NVARCHAR(100),
@Year INT
AS
BEGIN
    SELECT [ID], [Name], [Year], [Place of Birth] 
    FROM YourTable
    WHERE Name = @Name AND Year = @Year
END

所以你可以在下面搜索:

EXECUTE sp_search 'John', 1989

答案 1 :(得分:0)

首先用%替换输入中的空格,然后您可以执行以下操作:

DECLARE @input VARCHAR(1000) = REPLACE(@yourInput, ' ', '%')

SELECT *
FROM Table
WHERE CONVERT(VARCHAR(1000),Name) + 
      CONVERT(VARCHAR(1000),Year) + 
      CONVERT(VARCHAR(1000),[Place of Birth]) LIKE '%' + @input + '%'

这不会超级高效,因为它是

答案 2 :(得分:0)

我的桌子版本

DECLARE @yourTable TABLE (ID INT, Name VARCHAR(10),[Year] INT, [Place of Birth] VARCHAR(10));

INSERT INTO @yourTable
VALUES  (1,'John',1989,'Miami'),
        (2,'Steven',2004,'Orlando'),
        (3,'Cassandra',1990,'Chicago'),
        (4,'John',2000,'Orlando'),
        (5,'Elizabeth',1989,'Houston');

实际查询

DECLARE @input VARCHAR(100) = 'John 1989',
        @word1 VARCHAR(100),
        @word2 VARCHAR(100),
        @word3 VARCHAR(100),
        @yr INT;

--Separate words into different variables
SELECT  @word1 = SUBSTRING(@input,0,idx1),
        @word2 = SUBSTRING(@input,idx1 + 1,idx2-idx1),
        @word3 = SUBSTRING(@input,idx2 + 1,idx3-idx2)
FROM (VALUES(@Input + '   ')) A(input)
CROSS APPLY(SELECT CHARINDEX(' ',input,0        )) AS CA(idx1)
CROSS APPLY(SELECT CHARINDEX(' ',input,idx1 + 1))  AS CA2(idx2)
CROSS APPLY(SELECT CHARINDEX(' ',input,idx2 + 1))  AS CA3(idx3)

SELECT @yr      = (SELECT TOP (1) word FROM (VALUES(@word1),(@word2),(@word3)) A(word) WHERE ISNUMERIC(word) = 1 AND LEN(word) != 0);
WITH CTE_strings
AS
(
    SELECT word 
    FROM (VALUES(@word1),(@word2),(@word3)) A(word)
    WHERE ISNUMERIC(word) = 0 
    AND LEN(word) != 0 --length ignores white space so only grab values with at least 1 character
)

SELECT *
FROM @yourTable
WHERE   ([Year] = @yr OR @yr IS NULL) --match year unless it's empty
        AND
        (
            (SELECT COUNT(*) FROM CTE_strings) = 0 --no strings. Only year is listed
            OR
            (
                (SELECT COUNT(*) FROM CTE_strings) = 1 --if one string then either name or place of birth needs to match
                AND 
                (
                    Name IN (SELECT word FROM CTE_Strings)
                OR [Place of Birth] IN (SELECT word FROM CTE_Strings)
                )
            )
            OR
            (
                (SELECT COUNT(*) FROM CTE_strings) = 2 --if two strings, then both name and place of birth need to be in the list
                AND Name IN (SELECT word FROM CTE_Strings)
                AND [Place of Birth] IN (SELECT word FROM CTE_Strings)
            )
        )