这是从我的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" AND
GROUP BY organisation, Tour_ID
HAVING LEAST(COALESCE(MIN(A), "2119-01-01"),
COALESCE(MIN(B), "2119-01-01"),
COALESCE(MIN(C), "2119-01-01"),
COALESCE(MIN(D), "2119-01-01")) >= "2018-05-01 00:00" AND
LEAST(COALESCE(MIN(A), "2119-01-01"),
COALESCE(MIN(B), "2119-01-01"),
COALESCE(MIN(C), "2119-01-01"),
COALESCE(MIN(D), "2119-01-01")) < "2018-05-31 00:00"
) tt ON
tt.Tour_ID = t.Tour_ID AND
tt.organisation = t.organisation'
dbGetQuery(conn = connection, statement = cond)
我得到的错误消息是:
Error in .local(conn, statement, ...) : could not run statement:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near 'GROUP BY organisation, Tour_ID HAVING LEAST(COALESCE(MIN(A' at line 1
但是我看不到GROUP BY部分的问题!我的语法出了什么问题?