I'm attempting to build bread crumbs from a self referencing table. The query works perfectly fine in MySQL Workbench, but when run in application, the query fails with
Unhandled Exception: MySql.Data.MySqlClient.MySqlException: 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 ':= Parent
FROM
Codebook
WHERE
id = _id
) as ParentId,
' at line 6
I have looked for the right syntax for my server version (Percona Server (GPL), Release rel30.2, Revision 38.2). I'm hosting the DB off of GoDaddy. The query is as follows:
var query = @"SELECT T2.* FROM (
SELECT
@r as _id,
(
SELECT
@r := Parent
FROM
Codebook
WHERE
id = _id
) as ParentId,
@l := @l + 1 as lvl
FROM
Codebook
WHERE
@r <> 0
) T1
JOIN Codebook T2
ON T1._id = T2.Id
ORDER BY T1.lvl DESC";
return Query(query, new
{
r = childId,
l = 0
});
Where Query(query
is a wrapper method for dapper that uses a connection string stored in the appsettings of the website.
What am I doing wrong? Is there anyway I can be doing it better?