5

面白い結果が出ましたが、その理由がわかりません。

SELECT * 
  FROM items im
 WHERE ItemNumber

ItemNumberですvarchar(50)

このクエリの結果はItemNumber、数字で始まるすべてのアイテムを返します。ItemNumber文字で始まる場合は除外されます。

クエリがこのように相互作用する理由を誰かが説明していますか?

4

5 に答える 5

6

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.

于 2012-07-12T13:41:45.930 に答える
1

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.
于 2012-07-12T13:45:22.643 に答える
0

MySQLは、削除する必要があるこのブール値をサポートしています。それは混乱を招き、間違った/予期しない結果を返します。他のRDBMSはこれをサポートしていません。パラメータを使用したプロシージャがあり、以下のように指定するのを忘れたとします。

WHERE col 

すべてのデータが返され、デバッグが非常に困難です

于 2012-07-12T13:47:02.777 に答える
0

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
于 2012-07-12T13:37:54.263 に答える
-1

Maybe you should compare ItemNumber with something. Or try

WHERE ItemNumber NOT NULL

or something like

WHERE ItemNumber != ''
于 2012-07-12T13:38:24.593 に答える