[英]Conditional date in where clause of mysql not filtering
I have written the code above which gets its values form a user.我已经编写了上面的代码,它从用户那里获取它的值。 Its working but the date filter isn't working.它的工作,但日期过滤器不工作。 Instead its displaying all the records in the table even when the dates are provided.相反,即使提供了日期,它也会显示表中的所有记录。
What could be the issue?可能是什么问题?
$exp_query = "SELECT DISTINCT property_name, property_id, Water_charges, bank_charges, charge_1_description, other_charges_1,
charge_2_description, other_charges_2, dates ";
$exp_query .="FROM expenses ";
$exp_query .="WHERE
1=1
AND (property_name= '".$property."'
OR dates BETWEEN '".$datefro."' AND '".$dateto."' )
";
There is difference between AND and OR, also how to use OR with AND condition. AND 和 OR 之间有区别,还有如何在 AND 条件下使用 OR。 You have used an OR condition in parentheses, check them.您在括号中使用了 OR 条件,请检查它们。
$exp_query = "SELECT DISTINCT property_name, property_id, Water_charges, bank_charges, charge_1_description, other_charges_1,
charge_2_description, other_charges_2, dates ";
$exp_query .="FROM expenses ";
$exp_query .="WHERE property_name= '".$property."'
AND (dates BETWEEN '".$datefro."' AND '".$dateto."' )";
Assuming you are using right condition in your MYSQL query假设您在MYSQL查询中使用了正确的条件
just use MYSQL DATE()
function like::只需使用MYSQL DATE()
函数,例如:
$exp_query = "SELECT DISTINCT property_name, property_id, Water_charges, bank_charges, charge_1_description, other_charges_1,
charge_2_description, other_charges_2, dates ";
$exp_query .="FROM expenses ";
$exp_query .="WHERE
1=1
AND (property_name= '".$property."'
OR DATE( dates ) BETWEEN '".$datefro."' AND '".$dateto."' )
";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.