Feb
20
sql条件内排除写法:年龄大于18并且如果是女性又要大于28
define("CRITERIA_TOTAL_TYPE_ARRAY", "OVER_18, FEMALE_28");
根据此条件取出所有年龄超过18,并且如果性别是FEMALE>28的数据。
$criteria_sql = "";
if ($criteria_flg) {
$user_sql = "";
// OVER_18, FEMALE_28
$param_list = explode(",", CRITERIA_TOTAL_TYPE_ARRAY);
$over_age = explode("_", $param_list[0]);
if (isset($over_age[1])) {
$user_sql .= " and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $over_age[1] . " year")) . "'";
}
if (isset($param_list[1])) {
$sex_age = explode("_", $param_list[1]);
$sex = trim($sex_age[0]);
$user_sql .= " and (sex !='" . $sex . "' or (sex='" . $sex . "' and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $sex_age[1] . " year")) . "'))";
}
if (!empty($user_sql)) {
$criteria_sql = " inner join (select * from user_mst where delete_flg=false " . $user_sql . ") as u on (u.id=pad.user_id)";
}
}
这么漂亮的想法,后来客户更改需求了,但我要记录一下。
根据此条件取出所有年龄超过18,并且如果性别是FEMALE>28的数据。
$criteria_sql = "";
if ($criteria_flg) {
$user_sql = "";
// OVER_18, FEMALE_28
$param_list = explode(",", CRITERIA_TOTAL_TYPE_ARRAY);
$over_age = explode("_", $param_list[0]);
if (isset($over_age[1])) {
$user_sql .= " and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $over_age[1] . " year")) . "'";
}
if (isset($param_list[1])) {
$sex_age = explode("_", $param_list[1]);
$sex = trim($sex_age[0]);
$user_sql .= " and (sex !='" . $sex . "' or (sex='" . $sex . "' and date_trunc('day', birthday) < '" . date("Y-m-d", strtotime("-" . $sex_age[1] . " year")) . "'))";
}
if (!empty($user_sql)) {
$criteria_sql = " inner join (select * from user_mst where delete_flg=false " . $user_sql . ") as u on (u.id=pad.user_id)";
}
}
这么漂亮的想法,后来客户更改需求了,但我要记录一下。
作者:@Everyday NetLog
地址:http://blog.zhoz.com/read.php?765
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!