简体   繁体   中英

PHP-MySQL query "SELECT BETWEEN"

"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?

If you compare two integer values remove single quote for your query.

I never use max() function for performance, so the query is :

SELECT buildno FROM ".$tbl." 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. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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