简体   繁体   中英

How to add the list in glue_sql to Where filter

I'm writing a function and I wanted to paste a list typed by user to filters in WHERE. List typed to function by user:

filters = list( ‘fruits’ = c(‘apple’,’orange’),
‘vegetables’ = ‘carrot’)

How to paste this list to sql query in glue sql:

df = glue_sql(“select …
where filters)
parts <- paste(sapply(names(filters), function(nm) sprintf("%s in ({%s*})", nm, nm)), collapse = " and ")
parts
# [1] "fruits in ({fruits*}) and vegetables in ({vegetables*})"
glue::glue_data_sql(filters, paste("select * from table where", parts), .con = con)
# <SQL> select * from table where fruits in ('apple', 'orange') and vegetables in ('carrot')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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