我的尝试运行在Visual Studio asp.net的SQL查询,但我收到此错误:错误在SQL查询
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 '' AND JobID = jn.ID GROUP BY JOBID)) as Delivered, ' at line 1
但我无法看到问题的所在。
private static string SearchSQL
{
get{
return @" set @sql = null;
select
group_concat(distinct
concat(
'max(case when PieceType = ''',
PieceType,
''' then AdvisedQty end) AS ',
concat(PieceType)
)
)into @sql
FROM customer c
LEFT JOIN job_address ja ON c.AccountCode = ja.Code AND c.Company_ID = ja.Company_ID
JOIN AddressType jat ON ja.AddressType = jat.ID and jat.Description = ''Debtor''
LEFT JOIN job_new jn ON ja.JobID = jn.ID
LEFT JOIN job_pieces jp ON ja.JobID = jp.ID
WHERE c.Company_ID = ?compid;
set @sql = concat('select
c.Name,
COUNT(distinct jn.ID) as Jobs,
SUM((select COUNT(ID) from jobstat where Status = ''DEL'' AND JobID = jn.ID GROUP BY JOBID)) as Delivered,
SUM((select COUNT(ID) from jobstat where Status = ''POD'' AND JobID = jn.ID GROUP BY JobID)) as POD,
(select COUNT(job_debriefs.ID) from job_debriefs WHERE JobID = jn.JobNo) as Debriefs,
sum(jn.OutTurn) as Outturn,
SUM(jn.ActualWeight) as GrossWt,
SUM(jn.CBM) as CBM,
jn.Department,
(SELECT Name FROM job_address WHERE AddressType =3 AND JobID = jn.ID) as CollectName,
(SELECT Name FROM job_address WHERE AddressType =2 AND JobID = jn.ID) as DeliverName,
', @sql, '
FROM customer c
LEFT JOIN job_address ja ON c.AccountCode = ja.Code AND c.Company_ID = ja.Company_ID
JOIN AddressType jat ON ja.AddressType = jat.ID and jat.Description = ''Debtor''
LEFT JOIN job_new jn ON ja.JobID = jn.ID
LEFT JOIN job_pieces jp ON ja.JobID = jp.ID
WHERE c.Company_ID = ?compid
GROUP BY c.ID');
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
";
}
}
好吧,我固定的单引号,但现在的错误上来就是:
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 '?compid GROUP BY c.ID' at line 19
所以那不是问题
如果您尝试直接在DBMS中运行SQL代码,则可能会收到更有用的错误消息。 –
@TheTTGGuy我试着在MySQL Workbench中运行查询,但发生错误:'错误代码:1064.您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在'max(PieceType ='Carton'然后AdvisedQty结束时的情况)'时使用正确的语法。AS纸箱,最大(在'13号线'时的情况' – user123456789
是有原因的你应该只需要一个(这可能不是问题,但是可以用单个的parens来尝试) – nomistic