是否可以在 MySql 命令中使用 MySql 赋值运算符 (:=)?
Is it possible to use MySql assignment operator (:=) in a MySqlCommand?
我有一个查询在 MySql workbench 中运行良好,但在 运行 通过 MySqlCommand.ExecuteReader 时产生语法错误 "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"
。所讨论的语法是 select(即 SELECT @r := @r + 1
)内的变量赋值。我正在使用 .net connector v.6.9.9 以防相关。另外,这是我的连接字符串:"server=mymysqlserveraddress;port=3306;database=dbName;user id=username;password=********;Convert Zero Datetime=True;Allow User Variables=true"
我已尝试在连接字符串中设置 Allow User Variables=true,如下所示:Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?
我寻找了与此匹配的其他问题,但最终只找到了一个相同的问题,但对于 java 中的休眠:How can I use MySQL assign operator(:=) in hibernate native query?
这是在 MySql Workbench 中工作的原始 sql 查询:
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;
408 和 228000 只是示例 - 我的 C# 代码构造字符串,然后通过以下方式将其发送到服务器:
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...
}
}
构造查询示例(我在发送前将最终查询打印到日志中)因上述语法错误而失败:
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;
如您所见,构造的查询与我可以 运行 在 MySql workbench.
中没有错误的查询相同
@Uueerdo 的评论促使我尝试从 C# 代码中删除 cmd.Parameters.AddWithValue("r", 0);
行。这修复了错误并给了我预期的结果。
我有一个查询在 MySql workbench 中运行良好,但在 运行 通过 MySqlCommand.ExecuteReader 时产生语法错误 "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"
。所讨论的语法是 select(即 SELECT @r := @r + 1
)内的变量赋值。我正在使用 .net connector v.6.9.9 以防相关。另外,这是我的连接字符串:"server=mymysqlserveraddress;port=3306;database=dbName;user id=username;password=********;Convert Zero Datetime=True;Allow User Variables=true"
我已尝试在连接字符串中设置 Allow User Variables=true,如下所示:Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?
我寻找了与此匹配的其他问题,但最终只找到了一个相同的问题,但对于 java 中的休眠:How can I use MySQL assign operator(:=) in hibernate native query?
这是在 MySql Workbench 中工作的原始 sql 查询:
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;
408 和 228000 只是示例 - 我的 C# 代码构造字符串,然后通过以下方式将其发送到服务器:
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...
}
}
构造查询示例(我在发送前将最终查询打印到日志中)因上述语法错误而失败:
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;
如您所见,构造的查询与我可以 运行 在 MySql workbench.
中没有错误的查询相同@Uueerdo 的评论促使我尝试从 C# 代码中删除 cmd.Parameters.AddWithValue("r", 0);
行。这修复了错误并给了我预期的结果。