无法按名称过滤

时间:2017-06-07 05:49:04

标签: sql

下面的查询工作正常,但我无法搜索vehicle_name。就像我想要搜索车辆名称31但是当我输入它时,搜索全部而不是仅搜索31。请帮忙

 SELECT Vehicle_Name
    ,Total_Weight
    ,DATE
    ,bilty_No
FROM harvesting H 
INNER JOIN Vehicle V 
ON H.Vehicle_ID = V.Vehicle_ID

UNION all

SELECT Vehicle_Name
    ,Total_Weight
    ,DATE
    ,Bilty_No
FROM Blade_Harvesting BH 
INNER JOIN Vehicle V 
ON BH.Vehicle_ID = V.Vehicle_ID 

2 个答案:

答案 0 :(得分:0)

你必须在两个选择陈述中放置条件

SELECT Vehicle_Name
    ,Total_Weight
    ,DATE
    ,bilty_No
FROM harvesting H 
INNER JOIN Vehicle V 
ON H.Vehicle_ID = V.Vehicle_ID
where Vehicle_Name like '31%'
UNION all

SELECT Vehicle_Name
    ,Total_Weight
    ,DATE
    ,Bilty_No
FROM Blade_Harvesting BH 
INNER JOIN Vehicle V 
ON BH.Vehicle_ID = V.Vehicle_ID 
where Vehicle_Name like '31%'

发送车辆名称AS参数

 SELECT Vehicle_Name
        ,Total_Weight
        ,DATE
        ,bilty_No
    FROM harvesting H 
    INNER JOIN Vehicle V 
    ON H.Vehicle_ID = V.Vehicle_ID
    where Vehicle_Name = @Vehicle_Name
    UNION all

    SELECT Vehicle_Name
        ,Total_Weight
        ,DATE
        ,Bilty_No
    FROM Blade_Harvesting BH 
    INNER JOIN Vehicle V 
    ON BH.Vehicle_ID = V.Vehicle_ID 
    where Vehicle_Name = @Vehicle_Name

答案 1 :(得分:0)

您可以尝试:

SELECT     Vehicle_Name,
           Total_Weight,
           DATE,
           bilty_No
FROM      (SELECT      Vehicle_Name,
                       Total_Weight,
                       DATE,
                       bilty_No
           FROM        harvesting H 
           INNER JOIN  Vehicle V ON H.Vehicle_ID = V.Vehicle_ID

           UNION ALL

           SELECT      Vehicle_Name,
                       Total_Weight,
                       DATE,
                       Bilty_No
           FROM        Blade_Harvesting BH 
           INNER JOIN  Vehicle V ON BH.Vehicle_ID = V.Vehicle_ID 
           ) s
Where      s.Vehicle_ID = 31