【问题标题】:sql keyword IN using RSQLite and DBI package使用 RSQLite 和 DBI 包的 sql 关键字 IN
【发布时间】:2022-08-06 19:36:17
【问题描述】:

使用RSQLite和DBI包组成查询时,不知道有没有办法使用SQL关键字IN?

我不认为 SQL 关键字 IN 当前已实现?

例如

## instead of:
dbGetQuery(con, \"SELECT * FROM mtcars WHERE cyl = :targetCyl\" , 
params = list( targetCyl = 4 ))

## I want to do the following:

dbGetQuery(con, \"SELECT * FROM mtcars WHERE cyl IN :targetCyl\" , 
params = list( targetCyl = c( 2, 4, 6 ) ))

    标签: sql r r-dbi rsqlite


    【解决方案1】:
    1. SQL 的 IN 运算符要求其参数在括号中,如

      select * from mtcars where cyl in (?,?,?)
      
    2. DBI 要求您实例化所需的括号数量,如

      targetCyl <- c( 2, 4, 6 )
      dbGetQuery(
        con, paste("SELECT * FROM mtcars WHERE cyl IN (",
                   paste(rep("?", length(targetCyl)), collapse = ","), ")"),
        params = as.list(targetCyl))
      

      仅供参考,这不是特定于 SQLite 或 RSQLite,通常是 DBI

    【讨论】:

      猜你喜欢
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多