简体   繁体   English

mysql查询不返回正确值?

[英]Rmysql query does not return the correct values?

I have a query in r, using rmysql package : 我在r中使用rmysql包进行查询:

sql.query = paste0("SELECT * FROM performance_db.pcTab WHERE 
                       fk_ID_calc = (SELECT ID_calc FROM performance_db.calculationTab WHERE
                       turbine_model = '",input$t1,"' AND 
                       op_mode = '",input$t2,"' AND 
                       date_entered ='",input$t3,"')
                       AND sensor = 'P_el'"
  )
    spc = fetch(dbSendQuery(conn, sql.query), n = -1)

this return a table : 这返回一个表:

head(spc)
  ID_pc voltage_side sensor Air_density v2.5 v3  v4  v5  v6   v7   v8   v9  v10  v11  v12  v13  v14  v15  v16  v17  v18  v19
1     1            0   P_el       1.225    0 44 184 404 721 1165 1686 2107 2272 2300 2300 2300 2300 2300 2300 2300 2300 2300
2     2            0   P_el       1.000   NA 33 155 346 622 1010 1486 1967 2224 2286 2294 2300 2300 2300 2300 2300 2300 2300
3     3            0   P_el       1.030   NA 35 161 358 642 1041 1529 1999 2235 2291 2297 2300 2300 2300 2300 2300 2300 2300
4     4            0   P_el       1.060   NA 37 167 369 661 1072 1571 2036 2249 2295 2300 2300 2300 2300 2300 2300 2300 2300
5     5            0   P_el       1.090   NA 39 173 381 681 1103 1612 2060 2256 2300 2300 2300 2300 2300 2300 2300 2300 2300
6     6            0   P_el       1.120   NA 42 178 392 701 1134 1649 2080 2263 2300 2300 2300 2300 2300 2300 2300 2300 2300

I would like to add additional filter on Air_density like : 我想在Air_density上添加其他过滤器,例如:

sql.query = paste0("SELECT * FROM performance_db.pcTab WHERE 
                       fk_ID_calc = (SELECT ID_calc FROM performance_db.calculationTab WHERE
                       turbine_model = '",input$t1,"' AND 
                       op_mode = '",input$t2,"' AND 
                       date_entered ='",input$t3,"')
                       AND sensor = 'P_el' AND Air_density = 1"
  )

This is return just one row contain the information for Air_density = 1 : 仅返回包含Air_density = 1的信息的一行:

ID_pc voltage_side sensor Air_density v2.5 v3  v4  v5  v6   v7   v8   v9  v10  v11  v12  v13  v14  v15  v16  v17  v18  v19
1     2            0   P_el           1   NA 33 155 346 622 1010 1486 1967 2224 2286 2294 2300 2300 2300 2300 2300 2300 2300

But when I try to filter base on other Air_densities It does not return anything. 但是,当我尝试基于其他Air_densities进行过滤时,它不会返回任何内容。 Like if in above mentioned query I put AND Air_density = 1.225 : 就像在上面提到的查询中,如果我将AND Air_density = 1.225放入:

head(spc)
 [1] ID_pc        voltage_side sensor       Air_density  v2.5         v3           v4           v5           v6          
[10] v7           v8           v9           v10          v11          v12          v13          v14          v15         
[19] v16          v17          v18          v19

I have no idea why it is happening !!! 我不知道为什么会这样!

FLOAT and DOUBLE data types are not precise. FLOATDOUBLE数据类型不精确。

If you save the value 1.225 it can be saved as 1.22500001 . 如果保存值1.225 ,则可以另存为1.22500001 And when you try to query all data with the value 1.225 , it won't find it, because it is saved as 1.22500001 . 当您尝试查询所有值为1.225数据时,将找不到它,因为它被保存为1.22500001

You should better use datatypes DECIMAL or NUMERIC or try to CAST the FLOAT values as DECIMAL like this: 你应该更好地利用数据类型DECIMALNUMERIC或尝试CASTFLOATDECIMAL是这样的:

CAST(Air_density as DECIMAL(10,3))

This is from the docs of MySQL: 这来自MySQL的文档:

11.2.3 Floating-Point Types (Approximate Value) - FLOAT, DOUBLE 11.2.3浮点类型(近似值)-FLOAT,DOUBLE

The FLOAT and DOUBLE types represent approximate numeric data values. FLOAT和DOUBLE类型表示近似数字数据值。 MySQL uses four bytes for single-precision values and eight bytes for double-precision values. MySQL将四个字节用于单精度值,并将八个字节用于双精度值。

For FLOAT, the SQL standard permits an optional specification of the precision (but not the range of the exponent) in bits following the keyword FLOAT in parentheses. 对于FLOAT,SQL标准允许在括号中的关键字FLOAT后面的位中指定精度(而不是指数的范围)的可选规范。 MySQL also supports this optional precision specification, but the precision value is used only to determine storage size. MySQL还支持此可选的精度规范,但精度值仅用于确定存储大小。 A precision from 0 to 23 results in a 4-byte single-precision FLOAT column. 从0到23的精度导致4字节单精度FLOAT列。 A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column. 从24到53的精度将导致8字节的双精度DOUBLE列。

MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). MySQL允许使用非标准语法:FLOAT(M,D)或REAL(M,D)或DOUBLE PRECISION(M,D)。 Here, (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. 在此,(M,D)表示总共可以存储多达M位的值,其中D位可以在小数点后。 For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. 例如,显示为FLOAT(7,4)的列看起来像-999.9999。 MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001. MySQL在存储值时执行四舍五入,因此,如果将999.00009插入FLOAT(7,4)列,则近似结果为999.0001。

Because floating-point values are approximate and not stored as exact values, attempts to treat them as exact in comparisons may lead to problems. 由于浮点值是近似值而不是存储为精确值,因此在比较中尝试将它们视为精确值可能会导致问题。 They are also subject to platform or implementation dependencies. 它们还受平台或实现依赖性的约束。 For more information, see Section B.5.4.8, “Problems with Floating-Point Values” 有关更多信息,请参见第B.5.4.8节“浮点值的问题”

For maximum portability, code requiring storage of approximate numeric data values should use FLOAT or DOUBLE PRECISION with no specification of precision or number of digits. 为了获得最大的可移植性,需要存储近似数值数据值的代码应使用FLOAT或DOUBLE PRECISION,而没有规定精度或位数。

https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html

This link should help you, too: 此链接也应为您提供帮助:

https://dev.mysql.com/doc/refman/5.7/en/problems-with-float.html https://dev.mysql.com/doc/refman/5.7/en/problems-with-float.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM