如何将data.frame传递到SQL“ IN”中R条件? [英] How to pass data.frame into SQL "IN" condition using R?

查看:118
本文介绍了如何将data.frame传递到SQL“ IN”中R条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从R中的CSV文件读取值列表,并尝试将这些值传递到SQL(dbGetQuery)的IN条件中。有人可以帮我这个忙吗?

I am reading list of values from CSV file in R, and trying to pass the values into IN condition of SQL(dbGetQuery). Can some one help me out with this?

library(rJava)
library(RJDBC)
library(dbplyr)
library(tibble)
library(DBI)
library(RODBC)
library(data.table)


jdbcDriver <- JDBC("oracle.jdbc.OracleDriver",classPath="C://Users/********/Oracle_JDBC/ojdbc6.jar")

jdbcConnection <- dbConnect(jdbcDriver, "jdbc:oracle:thin:Rahul@//Host/DB", "User_name", "Password") 

## Setting working directory for the data
setwd("C:\\Users\\**********\\Desktop")

## reading csv file into data frame
pii<-read.csv("sample.csv")

pii

 PII_ID
S0094-5765(17)31236-5
S0094-5765(17)31420-0
S0094-5765(17)31508-4
S0094-5765(17)31522-9
S0094-5765(17)30772-5
S0094-5765(17)30773-7

PII_ID1<-dbplyr::build_sql(pii$PII_ID)

PII_ID1

<SQL> ('S0094-5765(17)31236-5', 'S0094-5765(17)31420-0', 'S0094-5765(17)31508-4', 'S0094-5765(17)31522-9', 'S0094-5765(17)30772-5', 'S0094-5765(17)30773-7')


Data<-dbGetQuery(jdbcConnection, "SELECT ARTICLE_ID FROM JRBI_OWNER.JRBI_ARTICLE_DIM WHERE PII_ID in  ?",(PII_ID1))

预期:

ARTICLE_ID
12345
23456
12356
14567
13456

实际结果:

[1] ARTICLE_ID
<0 rows> (or 0-length row.names)


推荐答案

SQL您在 dbGetQuery 的第二个参数中传递的代码只是一个文本字符串,因此您可以使用 paste 或等效方法构造此代码。

The SQL code you pass in the second argument to dbGetQuery is just a text string, hence you can construct this using paste or equivalents.

您正在执行以下操作:

in_clause <- paste0("('", paste0(pii$PII_ID, collapse = "', '"), "')")

sql_text <- paste0("SELECT ARTICLE_ID 
              FROM JRBI_OWNER.JRBI_ARTICLE_DIM
              WHERE PII_ID IN ", in_clause)

 data <- dbGetQuery(jdbcConnection, sql_text)

但是,第一个 paste0 的确切形式取决于 PII_ID (我假设它是文本)以及如何在sql中表示这种格式(我假设单引号)。

However, the exact form of the first paste0 depends on the format of PII_ID (I have assumed it is text) and how this format is represented in sql (I have assumed single quotes).

请务必检查 sql_text 是有效的SQL,然后将其传递给 dbGetQuery

Be sure to check sql_text is valid SQL before passing it to dbGetQuery.

IM重要:仅当 pii 包含少量值(我建议少于10个)时,此方法才适用。如果 pii 包含大量值,则查询将非常大,并且运行速度将非常缓慢。如果您在 pii 中有很多值,那么更好的方法是按照@nicola的评论进行联接或半联接。

IMPORTANT: This approach is only suitable when pii contains a small number of values (I recommend fewer than 10). If pii contains a large number of values your query will be very large and will run very slowly. If you have many values in pii then a better approach would be a join or semi-join as per @nicola's comment.

这篇关于如何将data.frame传递到SQL“ IN”中R条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆