简体   繁体   中英

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:

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.

How would I go about doing this in a stored procedure? 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.

Have you considered making your query into a list so that instead of using "like" you are doing something 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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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