Domanda

I'm trying to do a query in rmysql to get data from a db. I have a list of idNumbers in a column that I want to match and pull records for from a database.

R data frame: df1

idNumColumnInR        saleAmt
  345                   22.34
  456                   44.53
  678                   77.87
  ...                    ...

I think it would be something like to go from db -> R:

select * from dataBase where idNum in (df1$idNumColumnInR).  

I could just use a for loop and go thru each row of the column but was curious if there's a better way.

È stato utile?

Soluzione

You don't need to use a loop to use paste().

For example:

df1 <- data.frame(idNumColumnInR=c(345,456,678), 
 saleAmt=c(22.34,44.53,77.87))

qry <- paste("SELECT * from dataBase where idNum in (",
 paste(df1$idNumColumnInR, collapse=","),")"  )
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top