[英]MySQL IF Condition in Where Clause
Is it possible to decide by IF Condition which Where Clause I want to choose.是否可以通过 IF 条件决定我要选择哪个 Where 子句。
Something like:就像是:
IF(DATE_FORMAT(DATE(akDate), '%a')='SAT', USE WHERECLAUSE1, USE WHERECLAUSE2)
This is the case you can still write using rather common WHERE
statement such as this:在这种情况下,您仍然可以使用相当常见的
WHERE
语句来编写,例如:
... WHERE
(
(DATE_FORMAT(DATE(akDate), '%a') = 'SAT')
AND
(WHERECLAUSE1)
)
OR
(
(DATE_FORMAT(DATE(akDate), '%a') != 'SAT')
AND
(WHERECLAUSE2)
)
where, of course, you should replace WHERECLAUSE1
and WHERECLAUSE2
with appropriate conditions.当然,您应该用适当的条件替换
WHERECLAUSE1
和WHERECLAUSE2
。
Easy, you should read on Boolean Algebra .很简单,你应该阅读布尔代数。 In your case, here we go:
在你的情况下,我们开始:
A = WhereClause1
B = WhereClause2
X = Choice
You need to select lines with X && A
OR lines with !X && B
.您需要选择带有
X && A
行或带有!X && B
。 So basically your expression will be: (X && A) || (!X && B)
所以基本上你的表达是:
(X && A) || (!X && B)
(X && A) || (!X && B)
. (X && A) || (!X && B)
。 Which leads to:这导致:
(
(Choice AND WhereClause1)
OR
((NOT Choice) AND WhereClause2)
)
$query = " SELECT apt.apt_id,apt.reg_id,apt.u_id,apt.b_id,apt.apt_bathroom,apt.apt_size,apt.apt_rent,apt.apt_desc,apt.negotiable,apt.apt_status,apt.govt_program,building.b_borough,building.b_zipcode,building.b_type,building.b_intersection,building.b_intersection2,building.b_desc FROM apt LEFT JOIN building ON apt.b_id = building.b_id WHERE apt.apt_status = 1 AND ";
if ($search_size != 'empty')
{
$query .= "apt.apt_size = '".$search_size."' ";
if ($search_borough != 'empty' || $search_zipcode != 'empty' )
{
$query .= " AND ";
}
}
if ($search_borough != 'empty')
{
$query .= "building.b_borough= '".$search_borough."' ";
if ($search_zipcode != 'empty')
{
$query .= " AND ";
}
}
if ($search_zipcode != 'empty')
{
$query .= "building.b_zipcode = '".$search_zipcode."' ";
}
$query .= "ORDER BY apt.apt_id DESC LIMIT $start,$perpage ";
$query_run = mysql_query ($query);
if (!$query_run)
{
echo 'Error In the Query limit.';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.