【问题标题】:Preparing character strings for sql in () query within dbGetQuery在 dbGetQuery 中为 sql in () 查询准备字符串
【发布时间】:2016-09-17 03:32:52
【问题描述】:

我编写了以下查询来研究一些用于我的分析的示例方法,并且这项工作很好,我不需要修改此代码。但是我需要理解"",; sep="" 的用法以及dbGetQuery 中的limit 语句。

df <- data.frame(Sample.Num = integer(),
                 Sample.Mean = integer(),
                 quant.01 = integer(),
                 quant.05 = integer(),
                 quant.10 = integer(),
                 quant.25 = integer(),
                 quant.50 = integer(),
                 quant.75 = integer(),
                 stringsAsFactors = FALSE)
df[1,] <- NA
for (i in 1:500){
  sdf <- dbGetQuery(con,"select col11,col23,col30
                    from schema.db.name
                    where col1 in ('value1')
                    and col2 in ('(v3) - value3')
                    and col3 in ('v0123 - value4')
                    order by random()*600000 limit 100")
  meansample <- mean(sdf$mileage,na.rm = TRUE)
  quant.01 <- quantile(sdf$mileage,na.rm = TRUE,probs = .01)
  quant.05 <- quantile(sdf$mileage,na.rm = TRUE,probs = .05)
  quant.10 <- quantile(sdf$mileage,na.rm = TRUE,probs = .10)
  quant.25 <- quantile(sdf$mileage,na.rm = TRUE,probs = .25)
  quant.50 <- quantile(sdf$mileage,na.rm = TRUE,probs = .50)
  quant.75 <- quantile(sdf$mileage,na.rm = TRUE,probs = .75)
dbDisconnect(con)

问题:

Col1,2 & 3 有字符值。现在我需要检查 col2 的所有 7 个值和 col3 的 9 个值是否相同。每当我使用 col1,2,3 的任何特定值时,我都需要将它们存储在数据框中,以便在循环结束并以第二个值重新启动之前执行一些操作。

因此需要学习:Add a dynamic value into RMySQL getQuery

我也研究过gsubshQuote。尽管有几种组合以及限制语句,但我无法概念化'", df[i], "';", sep = "" 的使用。如果它是一个本地数据库,我可以使用dplyr 包来运行循环。我知道seq_along() 需要一个数值,并且为了通过在 () 查询中设置正确的“正确值”的循环。我确实尝试过 paste0 和 paste arguments 以及 collapse="," 争论,但没有帮助。 我还从下面的帖子中尝试了lapply,但它在 () 查询中不起作用。

How to do dbGetQuery for loop in R

(v3) - value3, (v1) - value1 等是 col 中的实际元素。这是 col2、col3 等属性的代码和描述的组合。

【问题讨论】:

  • col2 in ('(v3) - value3') ... 这作为WHERE IN 条件毫无意义,而且它的行为肯定不会像你想的那样。
  • @Tim Biegeleisen 抱歉没有清楚地发布。在准备 in () 语句用于 where 子句时,您有什么办法可以解释我使用 '", df[i], "';", sep = "" 吗?
  • 如果您的意思是将col2 与文本(v3) - value 进行字面比较,那么条件是正确的,但您不需要将WHERE IN 与单个值一起使用。相反,你可以说WHERE col2 = '(v3) - value'
  • 谢谢。从 SQL 的角度来看,我知道 where 和 in 子句。我只是在寻找一些帮助来理解复杂的帖子 How to add a dynamic value into RMySQL getQuery 它有很多有用的概念,但是像我这样的笨蛋却没有得到它背后的知识。非常感谢

标签: mysql r


【解决方案1】:

假设您有一个查询并且想要替换其中的一些范围值。

sql <- "select col11,col23,col30
                    from schema.db.name
                    where col1 in (%s)
                    and col2 in (%s)
                    and col3 in (%s)
                    order by random()*600000 limit 100"

这里我将使用 sprintf 来做查询中的替换, 所以我在字符串中留下了标记 (%s)。 因此,让我们设置 3 个要测试的范围:

col1 <- 1:10
col2 <- c('a', 'f', 'z')
col3 <- c('name1', 'name2')

# create strings that are valid in SQL
col1_sql <- paste(col1, collapse = ',')
col2_sql <- paste0("'", col2, "'", collapse = ',')  # put quotes on strings
col3_sql <- paste0("'", col3, "'", collapse = ',')

# now substitute back in query
sql_new <- sprintf(sql,
                   col1_sql,  # strings we just constructed
                   col2_sql,
                   col3_sql
)

# print out the query
cat(sql_new)

以下是查询内容:

select col11,col23,col30
    from schema.db.name
    where col1 in (1,2,3,4,5,6,7,8,9,10)
    and col2 in ('a','f','z')
    and col3 in ('name1','name2')
    order by random()*600000 limit 100

【讨论】:

  • 谢谢@Data Munger 这对我理解 sprintf 很容易理解很有帮助
猜你喜欢
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 2023-02-08
  • 2012-07-12
  • 2011-10-25
  • 2010-09-17
相关资源
最近更新 更多