我的嘗試運行在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