An attempt to perform a SELECT query using a floating-point value as a condition often results in unexpected behavior. The following issue exemplifies this challenge:
<code class="sql">SELECT * FROM `table` WHERE `ident`='ident23' AND `price`='101.31';</code>
This query returns zero rows, even though the value 101.31 appears in the database for the price column. Removing the price condition returns the expected row.
To address this issue, consider the following solutions:
Casting the floating-point value to a DECIMAL type before comparing ensures proper precision:
<code class="sql">SELECT * FROM table WHERE CAST(price AS DECIMAL) = CAST(101.31 AS DECIMAL);</code>
Consider modifying the price column to the DECIMAL type, which provides better precision for monetary values:
ALTER TABLE `table` MODIFY COLUMN `price` DECIMAL;
The above is the detailed content of Here are a few question-based titles based on your provided text, each focusing on a different aspect of the issue: Option 1 (Focusing on the problem): * Why Does My MySQL SELECT Query Return No Res. For more information, please follow other related articles on the PHP Chinese website!