RMySQL: SQL 无法识别的语法错误
RMySQL: SQL Syntax error unidentifiable
这是我的 的连续内容。我在 R 中使用 RMySQL 包将 SQL suery 发送到 MySQL 数据库。该错误表明语法有问题,但我找不到错误部分。谁能帮我解决这个错误?
数据 table 如下所示:
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
不清楚你想要什么。但是 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
日期常量应使用单引号。我推测第二个常量应该是 '2018-06-01'
,如果你想要 5 月所有日期的范围。
这是我的
数据 table 如下所示:
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
不清楚你想要什么。但是 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
日期常量应使用单引号。我推测第二个常量应该是 '2018-06-01'
,如果你想要 5 月所有日期的范围。