面白い結果が出ましたが、その理由がわかりません。
SELECT *
FROM items im
WHERE ItemNumber
ItemNumber
ですvarchar(50)
。
このクエリの結果はItemNumber
、数字で始まるすべてのアイテムを返します。ItemNumber
文字で始まる場合は除外されます。
クエリがこのように相互作用する理由を誰かが説明していますか?
This is MySQL's strategy to be clever and assuming that it knows what you mean (as opposed to do what you write).
Any expression that evaluates to non-zero is considered true. Those items that start with a letter cannot (implicitely) be converted to a number thus it's considered zero and therfor "false". I would think that items that have the (character) value '0'
are also excluded.
The following statement for example will happily delete all rows in your table:
DELETE FROM foobar
WHERE 42;
You cannot turn this behaviour off. Not even in ANSI mode will MySQL throw a syntax expression.
MYSQL will return all rows with the ItemNumber starts with a number which is greater than equal to 0.5 and less than equal to -0.5
. It consider all number greater >= +/-0.5 as true
. And where
clause works based on true or false only.
true >= -0.5 > false < 0.5 <= true
Very smartly, mysql consider All >= +/-0.5
number as 1 (true). It takes true for rounded non-zero values.
IF you write
SELECT * FROM items im WHERE 1; // will return all rows.
SELECT * FROM items im WHERE 0.5; // will return all rows.
SELECT * FROM items im WHERE 0.4; // will return nothing.
SELECT * FROM items im WHERE 1 and 0.2; // will return nothing.
SELECT * FROM items im WHERE 0.3 or 0.5; // will return all rows.
MySQLは、削除する必要があるこのブール値をサポートしています。それは混乱を招き、間違った/予期しない結果を返します。他のRDBMSはこれをサポートしていません。パラメータを使用したプロシージャがあり、以下のように指定するのを忘れたとします。
WHERE col
すべてのデータが返され、デバッグが非常に困難です
Your WHERE clause is waiting for a BOOLEAN, I don't now how mysql manage numeric varchar, but I think mysql may assume numeric starting string are equivalent to true. It may interpret the query as
SELECT *
FROM items im
WHERE ItemNumber IS TRUE
Maybe you should compare ItemNumber with something. Or try
WHERE ItemNumber NOT NULL
or something like
WHERE ItemNumber != ''