Closed
Description
原因:MYSQL是5.6无法使用freesql的父子表功能,所以找了一段查询父子表的SQL。其中用到了“:=”再给变量赋值。这种写法在Navicat
中直接执行可以,但是用freesql去执行会报错
C#代码:
return this.Orm.Select<PartitionInfo>()
.WithSql(@"
-- set @ids := 88, @l := 0;
SELECT t1.*
FROM
(
SELECT
@ids AS _ids,
(
SELECT
@ids := GROUP_CONCAT(id)
FROM
partition_info
WHERE
FIND_IN_SET(ParentId, @ids)
# 表内查询条件(AND site_id = 2)
) AS cids,
@l := @l + 1 AS LEVEL
FROM
partition_info
-- (SELECT @ids := 0, @l := 0) b
WHERE
@ids IS NOT NULL
) as t,
partition_info as t1
WHERE
FIND_IN_SET(t1.id, t._ids)
ORDER BY
LEVEL DESC", new { ids = id, l = 0 })
.ToList();
错误信息:"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_CONCAT(id)\r\n FROM\r\n ' at line 11"
如果不传递变量,用拼sql的方式同样报错。同样的问题我在sqlsuger
上也遇到过,同样的错误,目前也不知道如何处理。
请问如果一定要在sql中用到':='操作,应该如何做呢?想要搞定这个问题,遇到过非常多次这个问题了
最终解:连接字符串中添加‘AllowUserVariables=True’