web-dev-qa-db-ja.com

SQL SELECT WHEREフィールドに単語が含まれている

私はこのような結果を返す選択を必要とします:

SELECT * FROM MyTable WHERE Column1 CONTAINS 'Word1 Word2 Word3'

そして、私はすべての結果を必要とします、すなわち、これは 'Word2 Word3 Word1'または 'Word1 Word3 Word2'またはこれら3つの他の組み合わせを含む文字列を含みます。

すべての単語が結果に含まれる必要があります。

381
Mario M

やや遅いですが、 any の単語を含めるための実用的な方法です。

SELECT * FROM mytable
WHERE column1 LIKE '%Word1%'
   OR column1 LIKE '%Word2%'
   OR column1 LIKE '%Word3%'

all の単語が存在する必要がある場合は、これを使用してください。

SELECT * FROM mytable
WHERE column1 LIKE '%Word1%'
  AND column1 LIKE '%Word2%'
  AND column1 LIKE '%Word3%'

もっと速くしたい場合は、全文検索を検討する必要があります。これは各データベースタイプに非常に特有のものです。

588
mvp

ある文字列が別の文字列の部分文字列かどうかを判断するためにLIKEを使用する場合は、検索文字列内のパターンマッチング文字をエスケープする必要があります。

あなたのSQL方言がCHARINDEXをサポートしているなら、代わりに使う方がずっと簡単です:

SELECT * FROM MyTable
WHERE CHARINDEX('Word1', Column1) > 0
  AND CHARINDEX('Word2', Column1) > 0
  AND CHARINDEX('Word3', Column1) > 0

また、これと、受け入れられた回答の方法は、Wordのマッチングではなく、サブストリングのマッチングのみを対象としています。したがって、たとえば、文字列'Word1word2Word3'はまだ一致します。

54
Sam

関数

 CREATE FUNCTION [dbo].[fnSplit] ( @sep CHAR(1), @str VARCHAR(512) )
 RETURNS TABLE AS
 RETURN (
           WITH Pieces(pn, start, stop) AS (
           SELECT 1, 1, CHARINDEX(@sep, @str)
           UNION ALL
           SELECT pn + 1, stop + 1, CHARINDEX(@sep, @str, stop + 1)
           FROM Pieces
           WHERE stop > 0
      )

      SELECT
           pn AS Id,
           SUBSTRING(@str, start, CASE WHEN stop > 0 THEN stop - start ELSE 512 END) AS Data
      FROM
           Pieces
 )

問い合わせ

 DECLARE @FilterTable TABLE (Data VARCHAR(512))

 INSERT INTO @FilterTable (Data)
 SELECT DISTINCT S.Data
 FROM fnSplit(' ', 'Word1 Word2 Word3') S -- Contains words

 SELECT DISTINCT
      T.*
 FROM
      MyTable T
      INNER JOIN @FilterTable F1 ON T.Column1 LIKE '%' + F1.Data + '%'
      LEFT JOIN @FilterTable F2 ON T.Column1 NOT LIKE '%' + F2.Data + '%'
 WHERE
      F2.Data IS NULL
17
Eduardo Cuomo

SELECT * FROM MyTable WHERE Column1 CONTAINS 'Word1 Word2 Word3'の代わりに、を追加します。

SELECT * FROM MyTable WHERE Column1 CONTAINS 'Word1 And Word2 And Word3'

詳細はこちら https://msdn.Microsoft.com/ja-jp/library/ms187787.aspx

_ update _

フレーズを選択するには、次のように二重引用符を使用します。

SELECT * FROM MyTable WHERE Column1 CONTAINS '"Phrase one" And Word2 And "Phrase Two"'

p.s.containsキーワードを使用する前に、まずテーブルで全文検索を有効にする必要があります。詳細については、こちらを参照 https://docs.Microsoft.com/ja-jp/sql/relational-databases/search/get-started-with-full-text-search

10
messed-up
SELECT * FROM MyTable WHERE 
Column1 LIKE '%Word1%'
AND Column1 LIKE '%Word2%'
AND Column1 LIKE  '%Word3%'

質問への編集に基づいてORANDに変更しました。

6
Jon Crowell

Oracle Database を使用している場合は、 contains queryを使用してこれを実現できます。クエリを含むよりも速いクエリが含まれています。

すべての単語が必要な場合

SELECT * FROM MyTable WHERE CONTAINS(Column1,'Word1 and Word2 and Word3', 1) > 0

単語が必要な場合

SELECT * FROM MyTable WHERE CONTAINS(Column1,'Word1 or Word2 or Word3', 1) > 0

列に _ context _ 型のインデックスが必要です。

CREATE INDEX SEARCH_IDX ON MyTable(Column) INDEXTYPE IS CTXSYS.CONTEXT
5
mirmdasif

あなただけの試合を見つけたいのなら。

SELECT * FROM MyTable WHERE INSTR('Word1 Word2 Word3',Column1)<>0

SQLサーバー :

CHARINDEX(Column1, 'Word1 Word2 Word3', 1)<>0

完全に一致するようにする例(';a;ab;ac;',';b;')は一致しません。

SELECT * FROM MyTable WHERE INSTR(';Word1;Word2;Word3;',';'||Column1||';')<>0
4
Joshua Balan

mS SQL Serverのフルテキストインデックスで "tesarus search"を使用してください。何百万ものレコードがある場合、これは検索で "%"を使用するよりもはるかに優れています。 tesarusは他よりもメモリ消費量が少ないです。この関数を検索してみてください:)

0
Daryl Arenas
SELECT * FROM MyTable WHERE Column1 Like "*Word*"

column1に部分的な値がWordを含むすべてのレコードが表示されます。

0
Jino

最善の方法は、テーブル内のカラムにフルテキストインデックスを作成し、LIKEの代わりにcontainsを使用することです。

SELECT * FROM MyTable WHERE 
contains(Column1 , N'Word1' )
AND contains(Column1 , N'Word2' )
AND contains(Column1 , N'Word3' )
0
MiladAhmadi

この問題で言及されていることを達成するための最も簡単な方法の1つは、NEARまたは '〜'とともに _ contains _ を使用することです。たとえば、次のクエリはWord1、Word2、Word3を含むすべての列を取得します。

SELECT * FROM MyTable WHERE CONTAINS(Column1, 'Word1 NEAR Word2 NEAR Word3')

SELECT * FROM MyTable WHERE CONTAINS(Column1, 'Word1 ~ Word2 ~ Word3')

さらに、CONTAINSTABLEは、 "Word 1"、 "Word 2"、および "Word 3"の近接度に基づいて各文書のランクを返します。たとえば、ある文書に「The Word 1 is Word 2 and Word 3」という文が含まれている場合、他の文書よりも用語が互いに近いため、その順位は高くなります。 

もう1つ追加したいことは、列フレーズ内で単語が特定の距離内にある列を検索するためにproximity_termを使用できることです。

0

これを使用する場合は、理想的にはSQL Serverの全文検索を使用して実行する必要があります。

-- table to search in
CREATE TABLE dbo.myTable
    (
    myTableId int NOT NULL IDENTITY (1, 1),
    code varchar(200) NOT NULL, 
    description varchar(200) NOT NULL -- this column contains the values we are going to search in 
    )  ON [PRIMARY]
GO

-- function to split space separated search string into individual words
CREATE FUNCTION [dbo].[fnSplit] (@StringInput nvarchar(max),
@Delimiter nvarchar(1))
RETURNS @OutputTable TABLE (
  id nvarchar(1000)
)
AS
BEGIN
  DECLARE @String nvarchar(100);

  WHILE LEN(@StringInput) > 0
  BEGIN
    SET @String = LEFT(@StringInput, ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput) - 1, -1),
    LEN(@StringInput)));
    SET @StringInput = SUBSTRING(@StringInput, ISNULL(NULLIF(CHARINDEX
    (
    @Delimiter, @StringInput
    ),
    0
    ), LEN
    (
    @StringInput)
    )
    + 1, LEN(@StringInput));

    INSERT INTO @OutputTable (id)
      VALUES (@String);
  END;

  RETURN;
END;
GO

-- this is the search script which can be optionally converted to a stored procedure /function


declare @search varchar(max) = 'infection upper acute genito'; -- enter your search string here
-- the searched string above should give rows containing the following
-- infection in upper side with acute genitointestinal tract
-- acute infection in upper teeth
-- acute genitointestinal pain

if (len(trim(@search)) = 0) -- if search string is empty, just return records ordered alphabetically
begin
 select 1 as Priority ,myTableid, code, Description from myTable order by Description 
 return;
end

declare @splitTable Table(
wordRank int Identity(1,1), -- individual words are assinged priority order (in order of occurence/position)
Word varchar(200)
)
declare @nonWordTable Table( -- table to trim out auxiliary verbs, prepositions etc. from the search
id varchar(200)
)

insert into @nonWordTable values
('of'),
('with'),
('at'),
('in'),
('for'),
('on'),
('by'),
('like'),
('up'),
('off'),
('near'),
('is'),
('are'),
(','),
(':'),
(';')

insert into @splitTable
select id from dbo.fnSplit(@search,' '); -- this function gives you a table with rows containing all the space separated words of the search like in this e.g., the output will be -
--  id
-------------
-- infection
-- upper
-- acute
-- genito

delete s from @splitTable s join @nonWordTable n  on s.Word = n.id; -- trimming out non-words here
declare @countOfSearchStrings int = (select count(Word) from @splitTable);  -- count of space separated words for search
declare @highestPriority int = POWER(@countOfSearchStrings,3);

with plainMatches as
(
select myTableid, @highestPriority as Priority from myTable where Description like @search  -- exact matches have highest priority
union                                      
select myTableid, @highestPriority-1 as Priority from myTable where Description like  @search + '%'  -- then with something at the end
union                                      
select myTableid, @highestPriority-2 as Priority from myTable where Description like '%' + @search -- then with something at the beginning
union                                      
select myTableid, @highestPriority-3 as Priority from myTable where Description like '%' + @search + '%' -- then if the Word falls somewhere in between
),
splitWordMatches as( -- give each searched Word a rank based on its position in the searched string
                     -- and calculate its char index in the field to search
select myTable.myTableid, (@countOfSearchStrings - s.wordRank) as Priority, s.Word,
wordIndex = CHARINDEX(s.Word, myTable.Description)  from myTable join @splitTable s on myTable.Description like '%'+ s.Word + '%'
-- and not exists(select myTableid from plainMatches p where p.myTableId = myTable.myTableId) -- need not look into myTables that have already been found in plainmatches as they are highest ranked
                                                                              -- this one takes a long time though, so commenting it, will have no impact on the result
),
matchingRowsWithAllWords as (
 select myTableid, count(myTableid) as myTableCount from splitWordMatches group by(myTableid) having count(myTableid) = @countOfSearchStrings
)
, -- trim off the CTE here if you don't care about the ordering of words to be considered for priority
wordIndexRatings as( -- reverse the char indexes retrived above so that words occuring earlier have higher weightage
                     -- and then normalize them to sequential values
select s.myTableid, Priority, Word, ROW_NUMBER() over (partition by s.myTableid order by wordindex desc) as comparativeWordIndex 
from splitWordMatches s join matchingRowsWithAllWords m on s.myTableId = m.myTableId
)
,
wordIndexSequenceRatings as ( -- need to do this to ensure that if the same set of words from search string is found in two rows,
                              -- their sequence in the field value is taken into account for higher priority
    select w.myTableid, w.Word, (w.Priority + w.comparativeWordIndex + coalesce(sequncedPriority ,0)) as Priority
    from wordIndexRatings w left join 
    (
     select w1.myTableid, w1.priority, w1.Word, w1.comparativeWordIndex, count(w1.myTableid) as sequncedPriority
     from wordIndexRatings w1 join wordIndexRatings w2 on w1.myTableId = w2.myTableId and w1.Priority > w2.Priority and w1.comparativeWordIndex>w2.comparativeWordIndex
     group by w1.myTableid, w1.priority,w1.Word, w1.comparativeWordIndex
    ) 
    sequencedPriority on w.myTableId = sequencedPriority.myTableId and w.Priority = sequencedPriority.Priority
),
prioritizedSplitWordMatches as ( -- this calculates the cumulative priority for a field value
select  w1.myTableId, sum(w1.Priority) as OverallPriority from wordIndexSequenceRatings w1 join wordIndexSequenceRatings w2 on w1.myTableId =  w2.myTableId 
where w1.Word <> w2.Word group by w1.myTableid 
),
completeSet as (
select myTableid, priority from plainMatches -- get plain matches which should be highest ranked
union
select myTableid, OverallPriority as priority from prioritizedSplitWordMatches -- get ranked split Word matches (which are ordered based on Word rank in search string and sequence)
),
maximizedCompleteSet as( -- set the priority of a field value = maximum priority for that field value
select myTableid, max(priority) as Priority  from completeSet group by myTableId
)
select priority, myTable.myTableid , code, Description from maximizedCompleteSet m join myTable  on m.myTableId = myTable.myTableId 
order by Priority desc, Description -- order by priority desc to get highest rated items on top
--offset 0 rows fetch next 50 rows only -- optional paging

0
JBelfort