Rmysql查询没有返回正确的值?

时间:2017-11-16 08:36:36

标签: mysql sql r rmysql

我在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)

这会返回一张表:

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

我想在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"
  )

这只返回一行包含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

但是当我尝试根据其他Air_densities进行过滤时,它不会返回任何内容。就像在上面提到的查询中我放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

我不知道为什么会发生这种情况!!!

1 个答案:

答案 0 :(得分:1)

FLOATDOUBLE数据类型不准确。

如果您保存值1.225,则可将其保存为1.22500001。 当您尝试使用值1.225查询所有数据时,它无法找到它,因为它保存为1.22500001

您最好使用数据类型DECIMALNUMERIC 或尝试CASTFLOATDECIMAL设为CAST(Air_density as DECIMAL(10,3)) ,如下所示:

[ 65% 22783/34570] target thumb C++: updater <= bootable/recovery/updater/install.cpp
FAILED: /bin/bash -c "(PWD=/proc/self/cwd /usr/bin/ccache prebuilts/clang/host/linux-x86/clang-2690385/bin/clang++ -I system/extras/ext4_utils -I external/e2fsprogs/misc -I bootable/recovery/updater/.. -I /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/PACKAGING/updater_extensions_intermediates/ -I bootable/recovery/updater -I /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates -I /home/build/dfrascari/android7/AOSP/out/target/product/generic/gen/EXECUTABLES/updater_intermediates -I libnativehelper/include/nativehelper \$(cat /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/import_includes) -isystem system/core/include -isystem system/media/audio/include -isystem hardware/libhardware/include -isystem hardware/libhardware_legacy/include -isystem hardware/ril/include -isystem libnativehelper/include -isystem frameworks/native/include -isystem frameworks/native/opengl/include -isystem frameworks/av/include -isystem frameworks/base/include -isystem /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/include -isystem bionic/libc/arch-arm/include -isystem bionic/libc/include -isystem bionic/libc/kernel/uapi -isystem bionic/libc/kernel/common -isystem bionic/libc/kernel/uapi/asm-arm -isystem bionic/libm/include -isystem bionic/libm/include/arm -c    -fno-exceptions -Wno-multichar -msoft-float -ffunction-sections -fdata-sections -funwind-tables -fstack-protector-strong -Wa,--noexecstack -Werror=format-security -D_FORTIFY_SOURCE=2 -fno-short-enums -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Werror=date-time -DNDEBUG -g -Wstrict-aliasing=2 -DNDEBUG -UDEBUG  -D__compiler_offsetof=__builtin_offsetof -Werror=int-conversion -Wno-reserved-id-macro -Wno-format-pedantic -Wno-unused-command-line-argument -fcolor-diagnostics -nostdlibinc  -target arm-linux-androideabi    -target arm-linux-androideabi -Bprebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/arm-linux-androideabi/bin    -fvisibility-inlines-hidden -Wsign-promo  -Wno-inconsistent-missing-override -nostdlibinc  -target arm-linux-androideabi   -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing  -fno-rtti -DUSE_EXT4 -Wno-unused-parameter -fpie -D_USING_LIBCXX -std=gnu++14  -Werror=int-to-pointer-cast -Werror=pointer-to-int-cast  -Werror=address-of-temporary -Werror=null-dereference -Werror=return-type    -MD -MF /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.d -o /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.o bootable/recovery/updater/install.cpp ) && (cp /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.d /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.P; sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\\\\$//' -e '/^\$/ d' -e 's/\$/ :/' < /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.d >> /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.P; rm -f /home/build/dfrascari/android7/AOSP/out/target/product/generic/obj/EXECUTABLES/updater_intermediates/install.d )"
bootable/recovery/updater/install.cpp:165:13: error: use of undeclared identifier 'setfscreatecon'
            setfscreatecon(secontext);
            ^
bootable/recovery/updater/install.cpp:172:13: error: use of undeclared identifier 'setfscreatecon'
            setfscreatecon(NULL);
            ^
2 errors generated.
[ 65% 22783/34570] target thumb C++: updater <= bootable/recovery/updater/blockimg.cpp
ninja: build stopped: subcommand failed.
make: *** [ninja_wrapper] Error 1

这是来自MySQL的文档:

  

11.2.3浮点类型(近似值) - FLOAT,DOUBLE

     

FLOAT和DOUBLE类型表示近似数值数据值。   MySQL使用四个字节表示单精度值,使用八个字节表示   双精度值。

     

对于FLOAT,SQL标准允许可选的规范   精度(但不是指数的范围)以下的位数   括号中的关键字FLOAT。 MySQL也支持这个可选项   精度规格,但精度值仅用于   确定存储大小。从0到23的精度导致4字节   单精度FLOAT柱。从24到53的精度导致   8字节双精度DOUBLE列。

     

MySQL允许非标准语法:FLOAT(M,D)或REAL(M,D)或DOUBLE   PRECISION(M,d)。这里,(M,D)表示可以存储最多值   总共M位,其中D位可以在小数点后面。   例如,定义为FLOAT(7,4)的列将显示为-999.9999   何时显示。 MySQL在存储值时执行舍入,所以如果你   将999.00009插入FLOAT(7,4)列,大致结果为   999.0001。

     

因为浮点值是近似值而不是精确存储   价值,试图在比较中对待它们可能会导致   问题。它们也受平台或实施的约束   依赖。有关更多信息,请参见第B.5.4.8节“问题   浮点值“

     

为了获得最大的可移植性,需要存储近似数字的代码   数据值应使用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/problems-with-float.html