English 中文(简体)
我的SQL返回空洞,尽管它应该 t?
原标题:mySQL returns empty even though it shouldn t?
  • 时间:2012-04-10 12:08:59
  •  标签:
  • mysql

我全心全意是我的我的SQL新bie,但我不知道如何寻找答案,因此我为什么要在这里:

            DESCRIBE rParam;
            +----------------+---------------+------+-----+---------+-------+
            | Field          | Type          | Null | Key | Default | Extra |
            +----------------+---------------+------+-----+---------+-------+
            | p              | float         | YES  |     | NULL    |       |
            | d              | float         | YES  |     | NULL    |       |
            | LTP            | float         | YES  |     | NULL    |       |
            | LTD            | float         | YES  |     | NULL    |       |
            | alpha          | float         | YES  |     | NULL    |       |
            | N              | smallint(6)   | YES  |     | NULL    |       |
            | g              | float         | YES  |     | NULL    |       |
            | a              | float         | YES  |     | NULL    |       |
            | seed           | float         | YES  |     | NULL    |       |
            | startingWeight | float         | YES  |     | NULL    |       |
            | path           | varchar(1000) | YES  | UNI | NULL    |       |
            | type           | varchar(100)  | YES  |     | NULL    |       |
            +----------------+---------------+------+-----+---------+-------+
            12 rows in set (0.00 sec)


            SELECT p FROM rParam GROUP BY p;

            +--------+
            | p      |
            +--------+
            |      0 |
            |  0.001 |
            |  0.002 |
            |  0.003 |
            |  0.004 |
            |  0.005 |
            | 0.0075 |
            |  0.008 |
            |   0.01 |
            |  0.012 |
            | 0.0125 |
            |  0.014 |
            |  0.015 |
            |   0.02 |
            |  0.025 |
            |   0.03 |
            |  0.035 |
            |   0.04 |
            |   0.05 |
            |    0.1 |
            |    0.2 |
            |    0.3 |
            |    0.4 |
            |    0.5 |
            |    0.6 |
            |    0.7 |
            +--------+
            26 rows in set (0.00 sec)

我可以找到一种问题的结果:

            SELECT p FROM rParam WHERE p=0.5 GROUP BY p;
            +------+
            | p    |
            +------+
            |  0.5 |
            +------+
            1 row in set (0.00 sec)

然而,改变我所要求的价值:

            SELECT p FROM rParam WHERE p=0.6 GROUP BY p;
            Empty set (0.00 sec)

但我们可以从第一个产出中清楚地看到,有几行,p=0.6? 这对大多数珍贵阶层来说是一个问题——为什么我的SQL回了空套?

最佳回答

如同四舍五入的错误一样。 点数不能总是以双轨形式表示。 采用一些ep泥工艺:

SELECT DISTINCT p
FROM rParam
WHERE ABS(p - 0.6) < 0.00001

如果你想要准确的分数,则使用<代码>DECIMAL而不是FLOAT作为一栏数据类型。

页: 1 最好使用<代码>SlectT DISTINCT而不是GROUP BY

问题回答

The value 0.6 can not be stored exactly in a float, but 0.5 can. Float numbers are stored binary. 1/2 can be stored exaclty, also 1/4 and 3/4, 1/8, 3/8, 5/8 and 7/8. But 0.6 can not be written as a fraction with a power of 2 on the denominator. So it can not be stored exactly in binary formats.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签