簡體   English   中英

如何在R中的sqlQuery函數中使用REGEXP

[英]How to use REGEXP in sqlQuery function in R

編輯-1

我實際的數據庫是MSACCESS格式,我導入使用數據sqlQuery在功能RODBC R的的包以下是假的數據庫,我創建,這樣我可以使用提供可重復的例子RSQLite包。 我想在sqlQuery函數中使用regexp。

EDIT-1結束

以下是使用RSQLite包的模擬數據庫和關聯查詢。 REGEX (或REGEXP )功能不起作用,我無法弄清楚原因。

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}\' '))

有什么建議嗎?

我認為你的問題是在你有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}\' '))

請給我一些反饋,我對數據庫的工作量不大,但我認為這樣可以解決你的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM