简体   繁体   English

强制mySQL查询是R中不是数字的字符

[英]Force mySQL queries to be characters not numeric in R

I'm using RODBC to interface R with a MySQL database and have encountered a problem. 我正在使用RODBC将R与MySQL数据库连接并遇到问题。 I need to join two tables based on unique ID numbers (IDNUM below). 我需要根据唯一的ID号(下面的IDNUM)加入两个表。 The issue is that the ID numbers are 20 digit integers and R wants to round them. 问题是ID号是20位整数,R想要围绕它们。 OK, no problem, I'll just pull these IDs as character strings instead of numeric using CAST(blah AS CHAR). 好的,没问题,我只是使用CAST(blah AS CHAR)将这些ID作为字符串而不是数字。

But R sees the incoming character strings as numbers and thinks "hey, I know these are character strings... but these character strings are just numbers, so I'm pretty sure this guy wants me to store this as numeric, let me fix that for him" then converts them back into numeric and rounds them. 但R看到传入的字符串为数字,并认为“嘿,我知道这些是字符串......但这些字符串只是数字,所以我很确定这个人要我将它存储为数字,让我修复那对他来说“然后将它们转换成数字并将它们四舍五入。 I need to force R to take the input as given and can't figure out how to make this happen. 我需要强制R将输入视为给定,并且无法弄清楚如何实现这一点。

Here's the code I'm using (Interval is a vector that contains a beginning and an ending timestamp, so this code is meant to only pull data from a chosen timeperiod): 这是我正在使用的代码(Interval是一个包含开始和结束时间戳的向量,因此此代码仅用于从选定的时间段中提取数据):

test = sqlQuery(channel, paste("SELECT CAST(table1.IDNUM AS CHAR),PartyA,PartyB FROM 
table1, table2 WHERE table1.IDNUM=table2.IDNUM AND table1.Timestamp>=",Interval[1]," 
AND table2.Timestamp<",Interval[2],sep=""))

You will most likely want to read the documentation for the function you are using at ?sqlQuery , which includes notes about the following two relevant arguments: 您很可能希望阅读您正在使用的函数的文档?sqlQuery ,其中包含有关以下两个相关参数的说明:

as.is which (if any) columns returned as character should be converted to another type? as.is作为字符返回的哪些(如果有)列应该转换为另一种类型? Allowed values are as for read.table. 允许的值与read.table相同。 See 'Details'. 阅读详情'。

and

stringsAsFactors logical: should columns returned as character and not excluded by as.is and not converted to anything else be converted to factors? stringsAsFactors logical:应该将列作为字符返回并且不被as.is排除而不转换为其他任何内容转换为因子?

In all likelihood you want to specify the columns in questions in as.is . 您很可能希望在as.is指定问题中的列。

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

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