I have used the dbConnect and RMySQL packages to successfully connect to my company's database with R. However, I am struggling to run a query in R and am obtaining a frustrating error. The query works in the MySQL Workbench app that I use day to day, hence the frustration that it won't work in R. Here is a snippet of the SQL query:
USE mydb;
SELECT @theDate := '2017-05-03';
SELECT
@theDate AS today,
a.user_id AS user_id,
...
...
These are the first few lines of the query, and also the part of the query causing the error in R. I receive the following error:
my_query = " USE mydb;
SELECT @theDate := '2017-05-03';
SELECT
@theDate AS today,
a.user_id AS user_id,
...
... "
my_db = dbConnect(MySQL(), ...)
requested_query = dbSendQuery(my_db, my_query)
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 'SELECT
@theDate AS today,
a.user_id AS user_id,' at line 3
Sorry I cannot provide more reproducible code, but that would involve connecting to the company database and I can't share the database info.
Last comment - I believe the line has to do with the SELECT @theDate: := '2017-05-03'; line. This query was written by my coworker, not myself, and I hadn't before seen 2 select statements used like this before in a query. What's happening in the MySQL Workbench app is that @theDate is essentially a variable set to '2017-05-03'. The first column of the table returned by the query is all '2017-05-03'
Like I said, the query works in MySQL Workbench, but not in R. Quite frustrating. Any suggestions are appreciated!
Thanks!
EDIT - Realizing that this is more a question in trying to understand how setting variables works in SQL. Like I said I hadn't seen this before, but the first SELECT query is setting a variable and then the 2nd SELECT query uses that variable. Is this allowed? I wonder why its allowed in MYSQL Workbench but not R... still frustrating