4

编辑-1

我的实际数据库是 MSAccess 格式,我正在使用R 包中的sqlQuery函数导入数据RODBC。以下是我正在创建的假数据库,以便我可以使用RSQLite包提供可重现的示例。我想在函数中使用正则表达式 sqlQuery

EDIT-1结束

以下是使用RSQLite包的模拟数据库和关联查询。(REGEXREGEXP)函数不起作用,我不知道为什么。

data0 <- read.csv(textConnection(
'ID  value
P9W38   97
P9W39   17
P9W40   78
P9W41   7
P9W42_1 38
P9W42   13
P9W43   18
P9W44   76
P9W45   65
P9W46   24
P9W46_1 44
P9W47   8
P9W48   31
P9W49   82
P9W50   52
P9W50_2 55
P9W51   26
P9W52   33
P9W52_2 79
P9W53   67
P9W54   74
P9W55   55'
),sep='')

dbWriteTable(con, "Mydata", data0)

这些工作

dbGetQuery(con, paste0(' select * from Mydata where [ID] like \'P9W38\' '))
dbGetQuery(con, paste0(' select * from Mydata where [ID] like \'P9W42%\' '))

但是这些不起作用

dbGetQuery(con, paste0(' select * from Mydata where [ID] REGEX \'P9W(38|40|50)\' '))
dbGetQuery(con, paste0(' select * from Mydata where [ID] REGEX \'P9W(38|40|50)(_1){,1}\' '))

有什么建议吗?

4

1 回答 1

0

我认为您的问题在于您使用 REGEX 而不是 REGEXP 的查询:

http://dev.mysql.com/doc/refman/5.1/en/regexp.html

所以你的代码应该是这样的:

dbGetQuery(con, paste0(' select * from Mydata where [ID] REGEXP \'P9W(38|40|50)\' '))
dbGetQuery(con, paste0(' select * from Mydata where [ID] REGEXP \'P9W(38|40|50)(_1){,1}\' '))

请提供一些反馈,我对数据库的工作不多,但我认为这会解决你的问题

于 2013-07-09T11:18:38.317 回答