Search code examples
rselectrmysql

Using variables as table names in R and MySQL


how do I get a MySQL SELECT statement that takes a variable as table name.

As an example:

a = "table1"
b = "table2"

and so on...

Now I want to integrate these variables into the SELECT statement:

dbSendQuery(mydb, "select * from a")

I tried it with different comma variations but unfortunately without success. I also tried the percentage sign.

Can anyone tell me how I can solve this?

Thanks!


Solution

  • dbSendQuery(mydb, sprintf("select * from %s",a))
    

    sprintf should help in this case. For more info on how this works try ?sprintf