首页 > 解决方案 > 组合字母数字时如何选择真值?

问题描述

mysql:(customer_number = int无法更改)

$query = "SELECT * FROM clients WHERE customer_number= '123F'" 

结果应该是空的。

该查询的结果不正确,因为 MySQL 忽略了任何字母字符,这些字母字符构成具有整数数据类型的值的一部分,例如。'F'

标签: phpmysql

解决方案


You can check if the value provided is strictly integer.

$query = "SELECT * FROM clients WHERE customer_number= '123F' 
AND '123F' REGEXP '^-?[0-9]+$'";

AND needs both conditions compulsorily.

So, even if MySQL ignores F from 123F and treats it as 123, second condition will return FALSE and 123F will fail regular expression of strictly integer condition.

This will have following results:

123 -> pass

123F -> fail


推荐阅读