I have a query that works fine in MySql workbench, but is producing a syntax error "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 ':= 0+1 AS Rank, z.* 
FROM(SELECT MemberId, Valu' at line 2"
when run via MySqlCommand.ExecuteReader. The syntax in question is a variable assignment inside of a select (i.e. SELECT @r := @r + 1
). I am using .net connector v.6.9.9 in case that's relevant. Also, here's my connection string: "server=mymysqlserveraddress;port=3306;database=dbName;user id=username;password=********;Convert Zero Datetime=True;Allow User Variables=true"
I have tried setting Allow User Variables=true in the connection string as shown here: Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?
I looked for other questions that matched this but ultimately, only found one that is the same problem, but for hibernate in java: How can I use MySQL assign operator(:=) in hibernate native query?
Here is the raw sql query that works in MySql Workbench:
SELECT Rank, Value
FROM (SELECT @r := @r+1 AS Rank, z.*
FROM(SELECT MemberId, Value
FROM (SELECT Id, MemberId, Value, CreatedDate, EventId
FROM Scores
WHERE (LoadTime IS NULL AND EventId = 408)
ORDER BY Value DESC
) AS sub
)z,
(SELECT @r:=0)y
)x
WHERE x.Value<=228000;
The 408 and 228000 are just examples - my C# code constructs the string and then sends it to the server via:
StringBuilder statisticQueryString = new StringBuilder("");
// construct statisticQueryString
using (MySqlCommand cmd = new MySqlCommand(statisticQueryString.ToString(), conn))
{
cmd.Parameters.AddWithValue("r", 0);
using (MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
// Iterate through the collection of RankResult items...
}
}
A sample constructed query (I am printing the final query to the log before sending) that fails with the syntax error above is:
SELECT Rank, Value
FROM (SELECT @r := @r+1 AS Rank, z.*
FROM(SELECT MemberId, Value
FROM (SELECT Id, MemberId, Value, CreatedDate, EventId
FROM Scores
WHERE (LoadTime IS NULL AND EventId = 408)
ORDER BY Value DESC
) AS sub
)z,
(SELECT @r:=0)y
)x
WHERE x.Value<=228000;
As you can see, the constructed query is identical to the one I can run without errors in MySql workbench.