简体   繁体   English

SQL 查询并集两张表,通过多个关键字搜索多个列

[英]SQL Query union two tables, search multiple columns by multiple keywords

I am having difficulty in creating a query or stored procedure which can search my results by keyword.我在创建可以按关键字搜索结果的查询或存储过程时遇到困难。

My SQL Fiddle is here:我的 SQL 小提琴在这里:

http://sqlfiddle.com/#!18/48eaa8/1/0 http://sqlfiddle.com/#!18/48eaa8/1/0

I need to be able to allow my users to search by keywords.我需要能够允许我的用户按关键字搜索。

For example, in the above fiddle, the user should be able to enter 'sony colour' or 'sony 87154316459' to find results across multiple columns.例如,在上面的小提琴中,用户应该能够输入“sony colour”或“sony 87154316459”来跨多个列查找结果。

How would I go about doing this in a stored procedure?我将如何 go 在存储过程中执行此操作? I have edited my code for brevity so it is a lot more complex than this.为了简洁起见,我编辑了我的代码,所以它比这复杂得多。 Unfortunately it cannot be translated to SQL from Linq.不幸的是,它不能从 Linq 转换为 SQL。

Have you considered making your query into a list so that instead of using "like" you are doing something like:您是否考虑过将您的查询放入一个列表中,这样您就可以不使用“like”了,而是执行以下操作:

SELECT * FROM Product WHERE ManufacturerName In ('Sony','Monitor') OR ProductName in ('Sony','Monitor')

Based on your question update, the original concept holds and the stored proc would look like:根据您的问题更新,原始概念成立并且存储的过程如下所示:

Create Table Product (
  ProductId int,
  ManufacturerId varchar(50),
  ProductName varchar(50),
  Upc varchar(50)
);
Create Table ProductVariation (
  ProductVariationId int,
  ProductId int,
  VariationName varchar(50),
  Upc varchar(50)
);
Create Table Manufacturer (
  ManufacturerId int,
  ManufacturerName varchar(50)
);
GO
Create OR ALTER Procedure p_SearchProduct(@searchList varchar(max))
AS
    BEGIN
      DECLARE @searchValue varchar(max);
      set @searchValue = '%' + Replace(@searchList,' ','%') + '%';
      BEGIN
      with SearchList
      as
      (
      SELECT value as SearchItem
      FROM STRING_SPLIT(@searchList, ' ')  
      WHERE LTRIM(RTRIM(value)) <> ''  
      ), SearchData as
      (
        SELECT
        p.ProductId,
        NULL AS 'ProductVariationid',
        m.ManufacturerName,
        ProductName,
        UPC
        FROM Product p
        INNER JOIN Manufacturer m ON m.ManufacturerId = p.ManufacturerId
        UNION
        SELECT 
        v.ProductId,
        v.ProductVariationId,
        m.ManufacturerName,
        p.ProductName + ' (' + v.VariationName + ')' AS 'ProductName',
        v.UPC
        FROM ProductVariation v
        INNER JOIN Product p ON p.ProductId = v.ProductId
        INNER JOIN Manufacturer m ON m.ManufacturerId = p.ManufacturerId
      )
      SELECT * FROM SearchData p
      where p.ProductName like @searchValue
      OR exists ( select 'x' from SearchList where p.UPC like '%' + SearchItem + '%')
      END
    END
GO
insert into Manufacturer Values(1,'Sony');
insert into Product Values(1,1,'Sony Monitor (Variable)','');
insert into Product Values(2,1,'Sony Walkman','32164578121');
insert into ProductVariation Values(1,1,'32" Colour','87154316458');
insert into ProductVariation Values(1,1,'48" Colour','87154316459');

exec p_SearchProduct 'Sony 87154316458';

You would call it with你会用它来称呼它

exec p_SearchProduct 'sony colour';
OR
exec p_SearchProduct 'sony 87154316458';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM