首页 > 解决方案 > dbGetQuery 在命令中不将用户识别为数据库,而是将其识别为一些系统变量

问题描述

我正在尝试使用 PostgreSQL 连接到 R 中的数据库。但是,我对数据库用户的引号有疑问 - sql 将用户视为某种系统变量。因此在标准 sql 接口中我会使用

select count(*), email, name
from http_request_log hrl
inner join user_access_token uat on hrl.access_token_id = uat.id and uat.impersonated_by_id is null
inner join "user" u on uat.user_id = u.id
inner join customer c on u.customer_id = c.id
where hrl.created_time >= '2020-05-01'
group by u.email, c.name
order by count(*) desc

但是在 R 中,使用 dbGetQuery,引号存在问题 - 我不能使用“用户”,如果我更改为 ' 它不起作用 - 它仅在整个命令开始并以 ' 而不是“结束时才有效 - 但是然后where 子句不起作用,因为它无法识别日期

uzivatele <- dbGetQuery(con, 
                        "select count(*), email, name
                        from http_request_log hrl
                        inner join user_access_token uat on hrl.access_token_id = uat.id and uat.impersonated_by_id is null
                        inner join 'user' u on uat.user_id = u.id
                        inner join customer c on u.customer_id = c.id
                        where hrl.created_time >= '2020-05-01'
                        group by u.email, c.name
                        order by count(*) desc")

标签: sqlrpostgresql

解决方案


user是 PostgreSQL 中的保留字。

解决此问题的一种方法是在表名前加上模式名称,例如public.user在您的查询中。

或者您可以尝试\"user\"在您的查询字符串中。


推荐阅读