是否可以在MYSQL和“子句”中使用IF ELSE

时间:2013-01-27 08:40:20

标签: php mysql if-statement

下面我的例子"显然"没有工作:::有没有办法我可以得到 IF ELSE 或类似的工作:::我有几个PHP变量,基于这些变量我喜欢改变AND"条款"在此查询中,无需创建多个查询并使用PHP if语句来使用正确的查询块:::

$query = "SELECT $wpdb->posts.ID
          FROM $wpdb->posts 
          INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
          WHERE 1=1 
          AND $wpdb->posts.post_type = 'cars' 
          AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') 

          IF ( $drive )
          AND ( ($wpdb->postmeta.meta_key = 'vehicle_year' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$year')
          ELSEIF ( $fuel  )
          AND ( ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel')
          ENDIF

          AND ( ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel') )
          GROUP BY $wpdb->posts.ID 
          ORDER BY $wpdb->posts.post_date 
          DESC LIMIT 0, 99";

5 个答案:

答案 0 :(得分:2)

$p="";
$q="";
if( empty($drive) ){
    $p=" AND ( ($wpdb->postmeta.meta_key = 'vehicle_year' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$year')";
    }else{
        if( empty($fuel)  ){
            $q=" AND ( ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel')";
        }
    }

$query = "SELECT $wpdb->posts.ID
    FROM $wpdb->posts 
    INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
    WHERE 1=1 
    AND $wpdb->posts.post_type = 'cars' 
    AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') 
    ".$p.$q."
    AND ( ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel') )
    GROUP BY $wpdb->posts.ID 
    ORDER BY $wpdb->posts.post_date 
    DESC LIMIT 0, 99";

答案 1 :(得分:0)

您可以使用if

或者您可以在MYSQL中使用由case when组成的then, else end

答案 2 :(得分:0)

根据PHP条件设置局部变量,然后使用它们(局部变量)来准备SQL查询......

答案 3 :(得分:0)

试试这个:

$query = "SELECT $wpdb->posts.ID
      FROM $wpdb->posts 
      INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
      WHERE 1=1 
      AND $wpdb->posts.post_type = 'cars' 
      AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') 
      AND CASE WHEN $drive THEN $wpdb->postmeta.meta_key = 'vehicle_year' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$year'
               WHEN $fuel THEN $wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel'
               ELSE 1 = 1 END
      AND $wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel'
      GROUP BY $wpdb->posts.ID 
      ORDER BY $wpdb->posts.post_date 
      DESC LIMIT 0, 99";

答案 4 :(得分:0)

这在逻辑上是否相同?

"...
(
    $drive AND ($wpdb->postmeta.meta_key = 'vehicle_year' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$year')
    OR
    $fuel AND ($wpdb->postmeta.meta_key = 'vehicle_fuel' AND CAST($wpdb->postmeta.meta_value AS CHAR) = '$fuel')
)
..."