gpt4 book ai didi

mysql - 如何在 R 的 sqlQuery 函数中使用 REGEXP

转载 作者:可可西里 更新时间:2023-11-01 07:45:15 32 4
gpt4 key购买 nike

EDIT-1

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

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

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

关于mysql - 如何在 R 的 sqlQuery 函数中使用 REGEXP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946069/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com