Inner join on LIKE sqldf

て烟熏妆下的殇ゞ 提交于 2020-01-14 12:25:26

问题


How can I use the LIKE clause with an inner join using sqldf in R?

The code:

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)

And then when I run:

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

returns 0 rows.

Why? What are my other alternatives please. I apologise for making you create so many variables to run my code.


回答1:


The string concatenation operator is || in SQLite:

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

giving:

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

Any of these would also work:

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

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


来源:https://stackoverflow.com/questions/34332936/inner-join-on-like-sqldf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!