这是从我的previous question开始的。我在R中使用RMySQL包将SQL查询发送到MySQL数据库。该错误表明语法有问题,但是我找不到错误部分。有人可以帮我解决此错误吗?
数据表如下:
organisation Tour_ID A B C D
Ikea a 2018-04-01 2018-05-07 2018-05-09 2018-05-01
Ikea a 2018-06-01 2018-05-03 2018-05-29 NA
Ikea a 2018-04-02 2018-05-01 2018-07-08 2018-05-26
Ikea b 2018-06-02 2018-05-01 NA 2018-05-26
Ikea b 2018-06-02 2018-05-01 NA 2018-05-26
Ikea b NA 2018-05-05 2018-08-02 2018-06-01
Ikea c 2018-06-01 2018-05-07 2018-05-09 2018-05-01
Ikea c 2018-06-01 2018-05-03 NA NA
Ikea c 2018-08-02 2018-05-09 2018-07-08 2018-05-26
我写的语法是:
cond <- "SELECT t.* FROM myTable t JOIN
(SELECT organisation, Tour_ID,
FROM myTable WHERE organisation LIKE "Ikea"
GROUP BY organisation, Tour_ID
HAVING A >= \"2018-05-01 00:00\" AND
A < \"2018-05-31 00:00 \"
) tt ON
tt.Tour_ID = t.Tour_ID AND
tt.organisation = t.organisation"
dbGetQuery(conn = connection, statement = cond)
我得到的错误消息是:
Unknown column 'A' in 'having clause'
但是我发现HAVING部分没有问题!我的语法有什么问题?
另外,如果我在SELECT
子句中添加A,则错误消息显示为:
Error in .local(conn, statement, ...) :
could not run statement: Expression #3 of SELECT list is not in GROUP BY clause
and contains nonaggregated column 'myTable.myTable.A'
which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
答案 0 :(得分:1)
不清楚您想要什么。但是HAVING
中的列必须在GROUP BY
中或聚合的参数中。也许您想要:
(SELECT organisation, Tour_ID
FROM myTable WHERE organisation LIKE "Ikea"
GROUP BY organisation, Tour_ID
HAVING MIN(A) >= '2018-05-01' AND
MAX(A) < '2018-05-31'
) tt
单引号应用于日期常量。我想如果要给5月的所有日期都取一个范围,则第二个常数应该为'2018-06-01'
。