SQL根据第二张表中的搜索条件从第一张表中检索数据

时间:2019-05-27 09:14:15

标签: sql sql-server

我有2个表,我想根据第二个表上的位置条件从第一个表中获取数据。

PHARMASTOCK(1)      Pharma Products(2)
ID                  ProductID   
ProductID           GenericName 
TradeName           TradeName   
MRP                 Supplier    
Rate                Manufacturer    
Stock               PurchasePTR 
Batch               UnitRate    
Expiry              MRP 
StockSection    

这是我编写的SQL存储过程,但无法正常工作 符合预期!

@StockSupplier varchar(50), @StockManufacturer Varchar(50)
AS


SELECT  PharmaStock.ProductID,PharmaStock.TradeName,PharmaStock.MRP,Rate,Batch,Expiry,StockSection,Stock,Supplier,Manufacturer 
from PharmaStock 
    Full JOIN PharmaProducts ON PharmaStock.ProductID = PharmaProducts.ProductID 
where Supplier  like + @StockSupplier + '%' and Manufacturer  like + @StockManufacturer + '%'
order by Manufacturer asc

3 个答案:

答案 0 :(得分:0)

    SELECT  PharmaStock.ProductID, 
PharmaStock.TradeName, 
PharmaStock.MRP, 
PharmaStock.Rate, 
PharmaStock.Batch, 
PharmaStock.Expiry, 
PharmaStock.StockSection, 
PharmaProducts.Supplier, 
PharmaProducts.Manufacturer  
FROM PharmaStock 
        INNER JOIN  PharmaProducts ON PharmaStock.ProductID = PharmaProducts.ProductID WHERE PharmaProducts.Supplier  LIKE  '%%' and PharmaProducts.Manufacturer  LIKE  '%%' 
ORDER BY PharmaProducts.Manufacturer asc

为供应商和制造商传递字符串以进一步过滤联接结果。

答案 1 :(得分:0)

SELECT  PharmaStock.ProductID,PharmaStock.TradeName,PharmaStock.MRP,Rate,Batch,Expiry,StockSection,Stock,Supplier,Manufacturer 
    from PharmaStock 
        Full JOIN PharmaProducts ON PharmaStock.ProductID = PharmaProducts.ProductID 
    where Supplier  like CONCAT(@StockSupplier , '%') and Manufacturer  like CONCAT(@StockManufacturer ,'%') order by Manufacturer asc

在查询中更改了串联函数,然后更改了加号(需要删除之后的加号)。

答案 2 :(得分:0)

SELECT  PharmaStock.ProductID,PharmaStock.TradeName,PharmaStock.MRP,Rate,Batch,Expiry,StockSection,Stock,Supplier,Manufacturer 
from PharmaStock 
INNER JOIN PharmaProducts ON PharmaStock.ProductID = PharmaProducts.ProductID 
where Supplier  like  @StockSupplier + '%' and Manufacturer  like  @StockManufacturer + '%'
order by Manufacturer asc