简体   繁体   English

PHP-MySQL 查询“SELECT BETWEEN”

[英]PHP-MySQL query "SELECT BETWEEN"

"SELECT MAX(buildno) FROM ".$tbl." WHERE( day BETWEEN '$fd' and '$cd') and month='$cm'" "SELECT MAX(buildno) FROM ".$tbl." WHERE(day BETWEEN '$fd' and '$cd') and month='$cm'"

I am using the above query to select data between two days in a particular month , is this the correct way to incorporate PHP variables in SELECT BETWEEN query?我正在使用上述查询在特定月份的两天之间选择数据,这是在 SELECT BETWEEN 查询中合并 PHP 变量的正确方法吗?

If you compare two integer values remove single quote for your query.如果您比较两个整数值,请删除查询的单引号。

I never use max() function for performance, so the query is :我从不使用 max() 函数来提高性能,所以查询是:

SELECT buildno FROM ".$tbl." SELECT buildno FROM ".$tbl." WHERE ( day BETWEEN $fd and $cd ) AND month=$cm ORDER BY buildno DESC LIMIT 1 WHERE (day BETWEEN $fd and $cd ) AND month=$cm ORDER BY buildno DESC LIMIT 1

For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.为了在将 BETWEEN 与日期或时间值一起使用时获得最佳结果,请使用 CAST() 将值显式转换为所需的数据类型。 Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values.示例:如果将 DATETIME 与两个 DATE 值进行比较,请将 DATE 值转换为 DATETIME 值。 If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.如果在与 DATE 的比较中使用诸如“2001-1-1”之类的字符串常量,请将字符串转换为 DATE。

您将变量名作为字符串发送,您需要连接(就像您对 $tbl 所做的那样:

"SELECT MAX(buildno) FROM ".$tbl." WHERE day BETWEEN '".$fd."' and '".$cd."' and month='".$cm."'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM