当不满足条件参数时,为什么我的SQL会给我结果

时间:2019-06-06 13:20:50

标签: php sql

我有以下由PHP脚本执行的SQL查询。问题是,即使不满足$nameValue参数,它仍然会返回值。如何解决?我专门说:

WHERE ((driver_name = '$nameValue') AND
        status = 'FEDEX' or status = 'UPS')

AND仅在同时满足两个条件时返回,但仍显示状态为FEDEXUPS

的所有内容。
$sql =  "SELECT customer,
       sum( status = 'FEDEX' ) as PickedUpNUM,
       sum( status = 'UPS' ) as DeliveryNUM
        from Equipment
        WHERE ((driver_name = '$nameValue') AND
        status = 'FEDEX' or status = 'UPS')
        group by customer
        Order by DeliveryNUM DESC";

2 个答案:

答案 0 :(得分:1)

错误()位置

$sql =  "SELECT customer,
          sum( status = 'FEDEX' ) as PickedUpNUM,
          sum( status = 'UPS' ) as DeliveryNUM
          from Equipment
          WHERE driver_name = '$nameValue' AND
          (status = 'FEDEX' or status = 'UPS')
        group by customer
        Order by DeliveryNUM DESC";

答案 1 :(得分:0)

要使查询返回所需的结果,您需要进行一些更改

$sql =  "SELECT customer,
          sum( IF(status = 'FEDEX', 1, 0 ) as PickedUpNUM,
          sum( IF(status = 'UPS', 1, 0 ) as DeliveryNUM
          from Equipment
          WHERE driver_name = '$nameValue' AND
          (status = 'FEDEX' or status = 'UPS')
        group by customer
        Order by DeliveryNUM DESC";