如何用 SQL 中的给定单词进行所有组合

How to make all combinations with given words in SQL

我将使用 SQL 服务器进行搜索查询,搜索我的 table 中包含的每个单词。查询必须在以下情况下工作:

  1. 查询必须像 运算符一样在 colourstylemarerialshape 字段中搜索
  2. 查询必须搜索每个单独的搜索词(如 blueredmodernwood)以及这些词之间的所有可能组合。
  3. 搜索值必须包含在类似于 like 运算符的列中。

这意味着对于这个词,我只能找到带有[[colour like '%blue%'colour like '%red%']、material like '%wood%'style like '%modern%'和每个形状]的产品。 (要求输出)换句话说,每个单词的所有组合。

到目前为止,这是我的查询:

select distinct colour, style, material, shape
from products
where colour in ('blue', 'red', 'modern', 'wood') or
      style in ('blue', 'red', 'modern', 'wood') or
      material in ('blue', 'red', 'modern', 'wood') or
      shape in ('blue', 'red', 'modern', 'wood') ;

这是结果:

colour style material shape
Modern Aluminum Round
Modern Metal Round
Albast Modern Acrylic Round
Albast Modern Glass Cylinder
Albast Modern Glass Other
Albast Modern Glass Rectangle
Albast Modern Glass Round
Albast Modern Glass Square
Albast Modern Synthetic Material Globe
Albast Modern Synthetic Material Round
Amber Modern Steel Round
Black Cottage Wood
Black Cottage Wood Round
Black Modern Reflector
Black Modern Abs Round
Black Modern Acrylic Round
Black Modern Aluminum
Black Modern Aluminum Corner-Shaped
Black Modern Aluminum Cylinder
Black Modern Aluminum Half-Round
Black Modern Aluminum Other
Black Modern Aluminum Oval
Black Modern Aluminum Rectangle
Black Modern Aluminum Round
Black Modern Aluminum Square
Black Modern Cotton Hexagon
Black Modern Cotton Round
Black Modern Glass Rectangle

但我看到结果是基于一个或多个单词是否可以找到。

我也试过这个查询,但没有找到结果。

select distinct colour, style, material, shape
from products
where colour in ('blue', 'red', 'modern', 'wood') and
      style in ('blue', 'red', 'modern', 'wood') and
      material in ('blue', 'red', 'modern', 'wood') and
      shape in ('blue', 'red', 'modern', 'wood') ;

我找不到某个词是颜色、形状、样式还是 material。

更新:预期结果

colour style material shape
blue modern style wood round
red modern wood Rectangle
red modern wood round
blue modern wood Rectangle
blue modern wood globe
red modern wood globe

我认为没有干净的解决方案。这条语句可以完成这项工作:

select distinct colour, style, material, shape
from products 
where colour + style + material + shape like '%blue%' or
      colour + style + material + shape like '%red%' or
      colour + style + material + shape like '%modern%' or
      colour + style + material + shape like '%wood%';

经过一段时间的搜索,我找到了这个解决方案:

  • 我创建了一个带有一个参数的存储过程:@query
  • 然后我将查询拆分为空格。请参阅此问题的已接受答案:Splitting the string in sql server.
  • 然后我发表了我的select声明。
  • 对于查询的每个部分,我都添加了 where 子句并将 {0} 替换为我的查询部分。
  • 然后只需添加 1 = 1 即可获得有效的 SQL 查询。
  • 使用 EXEC 语句执行我生成的 SQL 查询。
  • 最后执行我的存储过程。

这是我的代码:

declare @sql nvarchar(max);
declare @q nvarchar(max);
declare @whereClause nvarchar(max);
declare @currentrow int = 1;

declare @totalqueries int = (select count(pn) 
                             from dbo.SplitString(' ', @query)); -- count how many query 
                                                                 -- parts I've got.

set @sql = 'select distinct colour, style, material, shape
            from products 
            where '; -- my select statement

set @whereClause = 'colour + style + material + shape like ''%{0}%'' and '; -- where clause

while @currentrow <= @totalqueries begin;

    select @q = s 
    from dbo.SplitString(' ', @query)
    where pn = @currentrow;

    set @sql = @sql + REPLACE(@whereClause, '{0}', @q); -- replacing `{0}` with my 
                                                        -- query part `@q`

    set @currentrow = @currentrow + 1;

end;

set @sql = @sql + ' 1 = 1;';

exec (@sql);

另请参阅此函数:

create FUNCTION dbo.SplitString (@sep nvarchar(1), @s varchar(4000))
    RETURNS table
AS
RETURN (
    WITH Pieces(pn, start, stop) AS (
        SELECT 1, 1, CHARINDEX(@sep, @s)
        UNION ALL
        SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
        FROM Pieces
        WHERE stop > 0
    )
    SELECT pn,
        SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS s
    FROM Pieces
)