LIKE sqldf上的内部联接

时间:2015-12-17 11:01:08

标签: r inner-join sqldf

如何在R中使用sqldf的内部联接使用LIKE子句?

代码:

Name <- c("Jack","Jill","Romeo")
Name <- as.data.frame(Name)
FullName <- c("School Jack H", "School Juliet G", "College Jill M", "College Romeo F")
Marks <- c("100","82","54","0")
FullBio <- cbind(FullName, Marks)
FullBio <-as.data.frame(FullBio)

然后当我跑:

sqldf("select a.*, b.* from Name a join FullBio b on a.Name like '%'+b.[FullName]+'%'") 

返回0行。

为什么呢?请问我有什么其他选择。我很抱歉让你创建了很多变量来运行我的代码。

1 个答案:

答案 0 :(得分:4)

SQLite中的字符串连接运算符为||

sqldf("select * from Name join FullBio on FullName like '%' || Name || '%'")

,并提供:

   Name        FullName Marks
1  Jack   School Jack H   100
2  Jill  College Jill M    54
3 Romeo College Romeo F     0

其中任何一个都可行:

sqldf("select * from Name join FullBio on instr(FullName, Name)")

sqldf("select * from Name join FullBio on like('%' || Name || '%', FullName)")