WHERE子句

时间:2017-04-08 14:53:33

标签: sql sql-server gridview case

我使用查询将结果发送到Gridview

SELECT DISTINCT 
    Order_Header.Id, Order_Header.Payment_Id, Order_Header.Order_No
FROM 
    Order_Header 
INNER JOIN
    Order_Shipping_Detail ON Order_Header.Id = Order_Shipping_Detail.Order_Id  
LEFT OUTER JOIN
    cod_orders ON cod_orders.Order_Id = Order_Header.Id
WHERE 
    Status = 2 

但是,我需要返回status = 2的所有订单和订单ID为cod(也是status = 2)和cod_orders.Is_confirmed = 1的订单。

我可以为此使用CASE语句吗?

但是,如果我尝试这样做,我就能实现我的成果。

SELECT DISTINCT Order_Header.Id, order_Header.Payment_Method_Id,Order_Header.Order_No
FROM Order_Header INNER JOIN Order_Shipping_Detail
ON Order_Header.Id = Order_Shipping_Detail.Order_Id 
LEFT JOIN cod_orders 
ON cod_orders.Order_Id = Order_Header.Id 
WHERE Status = 2 and Payment_Method_Id != 6
UNION 
Select DISTINCT Order_Header.Id, order_Header.Payment_Method_Id,Order_Header.Order_No
FROM Order_Header INNER JOIN Order_Shipping_Detail
ON Order_Header.Id = Order_Shipping_Detail.Order_Id 
LEFT JOIN cod_orders 
ON cod_orders.Order_Id = Order_Header.Id 
WHERE Status = 2 AND cod_orders.Is_Confirmed = 1

我可以将这些合并到一个查询中吗?

1 个答案:

答案 0 :(得分:0)

我认为你可以合并这样的两个条件

SELECT DISTINCT Order_Header.Id, Order_Header.Payment_Id,Order_Header.Order_No
FROM Order_Header INNER JOIN
     Order_Shipping_Detail
     ON Order_Header.Id = Order_Shipping_Detail.Order_Id LEFT OUTER JOIN
     cod_orders
     ON cod_orders.Order_Id = Order_Header.Id 
WHERE Status = 2 and (Payment_Method_Id != 6 or cod_orders.Is_confirmed = 1)