sqlrrmysql

Sql query based on R values in datatable


I have few blank values in one of my columns in dataset. I need to make sql query to database with only this few id (86) that contain missing value.

I have in mind something like that (not just paste id to in statement):

SELECT x.id,
x.sent 
FROM x
WHERE x.id IN [my R vector with id]

Solution

  • glue_sql will expand vectors into a comma separated list if you use * after the variable name in the glue expression.

    glue::glue_sql("
      SELECT x.id,
      x.sent 
      FROM x
      WHERE x.id IN ({idVector*})
    ", .con = con)
    

    con is a DBI connection to your database