RPostgreSQL-根据ID将列插入另一个表

时间:2015-09-17 08:15:14

标签: r rpostgresql

我已经使用RPostgreSQL连接R和postgresQL,我想根据“pid”将列插入另一个表中,请告知如何使用R命令实现:

>library(RPostgreSQL)
>drv<-dbDriver("PostgreSQL")
>itemlist<- dbGetQuery(con, "SELECT * from project_budget_itemlist")
>View(itemlist)
 pid item cost
  1   ABC  9
  2   ACB  8
  3   BAC  7
  3   ZZZ  6

另一张表格如下:

  >name<- dbGetQuery(con, "SELECT * from namelist")
  >View(name)

  pid name 
   1  Sally
   2  Joy
   3  Susan

我希望结果是:

   pid item cost name 
    1   ABC  9   Sally
    2   ACB  8   Joy
    3   BAC  7   Susan
    3   ZZZ  6   Susan

1 个答案:

答案 0 :(得分:0)

如果两个输出中没有匹配的pid,则合并将返回空数据帧。如果有,那么这应该有效:

STRINGLIT
 : '"' ( ( '\\' [\\bftrn"] ) | ~[\\\r\n"] )* '"'
   {
     // Get all the text that this rules matched
     String matched = getText();

     // Strip the first and the last characters (the quotes)
     String matchedWithoutQuotes = matched.substring(1, matched.length() - 1);

     // possibly do some more replacements here like replace `\\n` with `\n` etc.

     // Set the new string to this token
     setText(matchedWithoutQuotes);
   }
 ;