简体   繁体   English

结合字母数字时如何选择真实值?

[英]How to select true value when combining alphanumeric?

mysql: customer_number = int (cannot be changed) mysql: customer_number = int (无法更改)

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

The RESULT should be empty. 结果应该为空。

The query has a result which is incorrect because, MySQL ignore any alphabet character which form part of the value with an integer datatype, eg.'F' 查询的结果是不正确的,因为MySQL忽略了构成整数数据类型值的任何字母字符,例如'F'

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. AND必须同时满足两个条件。

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. 因此,即使MySQL忽略了123F F并将其视为123 ,第二个条件也将返回FALSE并且123F将使严格整数条件的正则表达式失败。

This will have following results: 结果如下:

123 -> pass 123 >通过

123F -> fail 123F >失败

You can cast customer_number to string: 您可以将customer_number为字符串:

SELECT *
FROM clients
WHERE CAST(customer_number as CHAR(50)) = '123F'

The above query is of course not SARGable. 上面的查询当然不是SARGable的。 Consider processing the comparison argument on the client side before the construction of the query. 考虑在构造查询之前在客户端处理比较参数。

Demo here 在这里演示

Or check as first condition if the integer from the string equal the string (change the string to your fieldname) : 或首先检查字符串中的整数是否等于字符串(将字符串更改为您的字段名):

SELECT * FROM clients 
WHERE
  '123F'+0 = '123F' 
AND
  customer_number= '123F';

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

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