如何将if条件放在where子句中

时间:2018-11-16 07:56:19

标签: sql sql-server

请不要在问题IF clause within WHERE clause中重复? 我认为我的问题是与众不同,我已经尝试过,但是在以下情况下我无法成功。

如何在SQL中的哪个位置之间放置if条件 例如

        SELECT *
        FROM LocationOutsw W
        WHERE if(@p_type = 'ALL') then
            (W.NAME LIKE '%'+ @p_search + '%' OR W.DESCRIPTION LIKE '%'+ @p_search + '%')
            if(@p_type == "NAME")
              W.NAME LIKE '%'+ @p_search + '%'
            if(@p_type == "DESCRIPTION ")
              W.DESCRIPTION LIKE '%'+ @p_search + '%')

3 个答案:

答案 0 :(得分:3)

SELECT *
FROM LocationOutsw W
WHERE ( @p_type = 'ALL'
    AND ( W.NAME LIKE '%'+ @p_search + '%'
            OR W.DESCRIPTION LIKE '%'+ @p_search + '%'
        )       
) 
OR (    @p_type = 'NAME'
    AND W.NAME LIKE '%'+ @p_search + '%'
)
OR ( @p_type = 'DESCRIPTION'
    AND W.DESCRIPTION LIKE '%'+ @p_search + '%'
)

但是我强烈建议您不要像这样编写查询,因为您将获得错误的查询计划。

答案 1 :(得分:2)

我会为此使用布尔表达式

<?php  

class Vehicles{
    private $noOfVehicles;
    private $color;
    private $fuel;
    private $speed;

    public function getNoOfVehicles(){
        return $this->noOfMobiles;
    }

    public function setNoOfVehicles($Vehicles){
        $this->noOfMobiles = $Vehicles;
        echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
    }

    public function getColor(){
        return $this->color;
    }

    public function setColor($look){
        $this->color = $look;
        echo "</br>The Color of Vehicle is: ".$this->color."</br>";
    }

    public function getFuel(){
        return $this->fuel;
    }

    public function setFuel($petrol){
        $this->fuel = $petrol;
        echo "</br>The fuel is: ".$this->color."</br>";
    }

    public function getSpeed(){
        return $this->speed;
    }

    public function setSpeed($vehicleSpeed){
        $this->speed = $vehicleSpeed;
        echo "</br>The speed of vehicle is: ".$this->speed."</br>";
    }

}

class PassengerVehicles extends Vehicles{
    private $passengerSeats;

    public function getPassengerSeats(){
        return $this->passengerSeats;
    }

    public function setPassengerSeats($seats){
        return $this->passengerSeats = $seats;
        echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
    }

}

class TransportationVehicles extends Vehicles{
    private $noOfDoors;
    private $loadCapacity;

    public function getNoOfDoors(){
        return $this->noOfDoors;
    }

    public function setNoOfDoors($doors){
        return $this->noOfDoors = $doors;
        echo "</br>The No of Doors are: ".$this->noOfDoors."</br>";
    }

    public function getLoadCapacity(){
        return $this->loadCapacity;
    }

    public function setLoadCapacity($capacity){
        return $this->loadCapacity = $capacity;
        echo "The Load Capacity is: ".$this->loadCapacity."</br>";
    }
}

$VehiclesObj = new Vehicles;
$VehiclesObj->setNoOfVehicles("15");
$VehiclesObj->setColor("Black");
$VehiclesObj->setFuel("5 Litre");
$VehiclesObj->setSpeed("120 km/h");

$VehiclesObj->setPassengerSeats("4");
$VehiclesObj->setNoOfDoors("4");
$VehiclesObj->setLoadCapacity("500 KG");

?>

在语法上要小心。撇号的含义与引号不同。

答案 2 :(得分:2)

我会推荐这种方式。它将做得更好..

SELECT 
  *
FROM 
  LocationOutsw W
WHERE 
   (@p_type = 'ALL' AND
   (W.NAME LIKE '%'+ @p_search + '%'  OR  W.DESCRIPTION LIKE '%'+ @p_search + '%'))
   OR
   (@p_type = 'NAME' AND W.NAME LIKE '%'+ @p_search + '%')
   OR
   (@p_type = 'DESCRIPTION' AND W.DESCRIPTION LIKE '%'+ @p_search + '%')

'的单引号而不是"的双引号。