3

Let's say we have a record in table 'orders' with id=1. This query:

SELECT * FROM 'orders' WHERE id = 'abc1'

won't return mentioned record. But this query:

SELECT * FROM 'orders' WHERE id = '1abc'

will return the record with id=1. This is because when MySQL converts string to number 'abc1' becomes 0, but '1abc' becomes 1. Is there a nice way to make MySQL search strictly for records with id from query, i.e. not return the record with id=1 in both mentioned cases?

4

2 回答 2

6

如何使用:

SELECT * FROM 'orders' WHERE id LIKE '1abc' COLLATE utf8_bin

甚至

SELECT * FROM 'orders' WHERE STRCMP(id, '1abc') = 0;
于 2013-04-22T18:01:35.973 回答
0

我在提交 SQL 查询之前用 PHP 处理了它

$idTest = '1abc';
if (is_numeric($id){
    $query = "SELECT * FROM 'orders' WHERE id = '$idTest'"
}

$idTest如果有字符串,这将阻止提交查询

于 2018-12-08T10:40:46.107 回答