作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我下面的示例“显然”不起作用::: 有没有办法获得 IF ELSE 或类似的工作::: 我有几个 PHP 变量,基于这些变量我想更改此查询中的 AND“子句”,而不必创建多个查询并使用 PHP if 语句来使用正确的查询 block :::
$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";
最佳答案
$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";
关于php - 是否可以在 MYSQL AND "clause"中使用 IF ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14545794/
我是一名优秀的程序员,十分优秀!