使用Like运算符创建内部联接

时间:2018-12-10 09:53:53

标签: sql sql-server

我正在使用SQL Server DBMS在java fx中开发常规商店管理。我必须将数据存储在表中,以便为此目的必须放置一个文本字段来搜索数据,而我必须通过数据库中的SQL搜索:

CREATE PROC Spgetproductlistwithsearch @value NVARCHAR(max) 
AS 
  BEGIN 
      SELECT p.product_id, 
             p.NAME, 
             pt.type, 
             p.pur_price, 
             p.sale_price, 
             p.stock, 
             s.NAME, 
             s.contact_no, 
             s.email 
      FROM   ((product AS p 
               INNER JOIN producttype AS pt 
                       ON p.type_id = pt.type_id) 
              INNER JOIN supplier AS s 
                      ON p.supplier_id = s.supplier_id) 
      WHERE  p.NAME LIKE @value 
              OR s.NAME LIKE @value 
              OR pt.type LIKE @value 
              OR p.pur_price LIKE @value 
              OR p.sale_price LIKE @value 
              OR p.stock LIKE @value 
              OR p.product_id LIKE @value 
              OR s.contact_no LIKE @value 
              OR s.email LIKE @value 
  END; 

EXEC Spgetproductlist

但是它没有给我期望值,而是给了我两个表,但我需要将所有这些表连接起来

1 个答案:

答案 0 :(得分:0)

尝试一下:

您可以检查以下链接以获取更多详细信息:Usinig like operator with stored procedures parameters

CREATE PROC spGetProductListWithSearch 
        @value nvarchar(MAX)
    AS
    BEGIN
        SELECT
            p.Product_id
           ,p.Name
           ,pt.Type
           ,p.Pur_Price
           ,p.Sale_Price
           ,p.Stock
           ,s.Name
           ,s.Contact_No
           ,s.Email
        FROM ((Product AS p
        INNER JOIN ProductType AS pt
            ON p.Type_Id = pt.Type_Id)
        INNER JOIN Supplier AS s
            ON p.Supplier_Id = s.Supplier_Id)
        WHERE p.Name LIKE '%'+@value+'%'
        OR s.Name LIKE '%'+@value+'%'
        OR pt.Type LIKE '%'+@value+'%'
        OR p.Pur_Price LIKE '%'+@value+'%'
        OR p.Sale_Price LIKE '%'+@value+'%'
        OR p.Stock LIKE '%'+@value+'%'
        OR p.Product_id LIKE '%'+@value+'%'
        OR s.Contact_No LIKE '%'+@value+'%'
        OR s.Email LIKE '%'+@value+'%'
    END
    GO
    EXEC spGetProductListWithSearch
        @value = 'test'