mysql提示错误#42000You have an error in your SQL syntax; check the manual that corresponds

时间:2021-09-27 15:51:39

今天遇到个问题,就是接着上个mysql多行转列产生的,

上篇文章写的没有问题,在mysql、sqlserver和  Navcate里面执行都对,可是到了vc  dialog程序的一个按键下,执行mysql就产生了问题,在网上找了好多原因,

基本上都是说  语句的字段名和 数据库的 固有词有重复(比如int  dec之类的),但是哥是一个老程序猿,怎么可能犯这种低级错误,扯淡么……今天最终解决了这个问题。


错误提示:

(#42000You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version 
for the right syntax to use near '?
MAX(CASE devTypeId WHEN 2 THEN devSN ELSE 0 END) AS 1显示器,
MAX(CASE dev' at line 1


原来语句:

CString strSql;
strSql.Format("SELECT MacSN AS 0整机号,\
MAX(CASE devTypeId WHEN 2 THEN devSN ELSE 0 END) AS 1显示器,\
MAX(CASE devTypeId WHEN 3 THEN devSN ELSE 0 END) AS 2条码阅读器,\
MAX(CASE devTypeId WHEN 4 THEN devSN ELSE 0 END) AS 3主板盒,\
MAX(CASE devTypeId WHEN 5 THEN devSN ELSE 0 END) AS 4打印机,\
MAX(CASE devTypeId WHEN 6 THEN devSN ELSE 0 END) AS 5打印头, \
MAX(CASE devTypeId WHEN 7 THEN devSN ELSE 0 END) AS 6固件版本号,\
MAX(CASE devTypeId WHEN 8 THEN devSN ELSE 0 END) AS 7电源,\
MAX(CASE devTypeId WHEN 9 THEN devSN ELSE 0 END) AS 8键盘, \
MAX(CASE compTypeId WHEN 10 THEN devSN ELSE 0 END) AS 9液晶屏, \
MAX(CASE compTypeId WHEN 11 THEN devSN ELSE 0 END) AS 10识别引擎, \
MAX(CASE compTypeId WHEN 12 THEN devSN ELSE 0 END) AS 11主板, \
MAX(CASE compTypeId WHEN 13 THEN devSN ELSE 0 END) AS 12内存, \
MAX(CASE compTypeId WHEN 14 THEN devSN ELSE 0 END) AS 13存储卡 \
FROM \
(SELECT a.ID,a.MacSN,b.devSN,b.devTypeId,b.Remark,c.CompSN,c.compTypeId,c.Remark AS cmpRK \
FROM t_machineinfo A LEFT JOIN t_deviceinfo B ON B.MacId = A.id \
LEFT JOIN t_componentinfo C ON B.id = C.devId ORDER BY a.id ) AS TMP \
GROUP BY MacSN ");
下面是,mysql_query(&mydbcon,strSql);// 正确返回0,错误返回1,这里就一直返回1,尼玛~~~

这条语句,明细应该是对的,怎么从数据库工具里执行都对,到vc程序里怎么就不对了呢。



然后,经多番删改测试,将上面语句修改如下执行,正确了:

CString strSql;
strSql.Format("SELECT MacSN AS '0整机号',\
MAX(CASE devTypeId WHEN 2 THEN devSN ELSE 0 END) AS '1显示器',\
MAX(CASE devTypeId WHEN 3 THEN devSN ELSE 0 END) AS '2条码阅读器',\
MAX(CASE devTypeId WHEN 4 THEN devSN ELSE 0 END) AS '3主板盒',\
MAX(CASE devTypeId WHEN 5 THEN devSN ELSE 0 END) AS '4打印机',\
MAX(CASE devTypeId WHEN 6 THEN devSN ELSE 0 END) AS '5打印头', \
MAX(CASE devTypeId WHEN 7 THEN devSN ELSE 0 END) AS '6固件版本号',\
MAX(CASE devTypeId WHEN 8 THEN devSN ELSE 0 END) AS '7电源',\
MAX(CASE devTypeId WHEN 9 THEN devSN ELSE 0 END) AS '8键盘', \
MAX(CASE compTypeId WHEN 10 THEN devSN ELSE 0 END) AS '9液晶屏', \
MAX(CASE compTypeId WHEN 11 THEN devSN ELSE 0 END) AS '10识别引擎', \
MAX(CASE compTypeId WHEN 12 THEN devSN ELSE 0 END) AS '11主板', \
MAX(CASE compTypeId WHEN 13 THEN devSN ELSE 0 END) AS '12内存', \
MAX(CASE compTypeId WHEN 14 THEN devSN ELSE 0 END) AS '13存储卡' \
FROM \
(SELECT a.ID,a.MacSN,b.devSN,b.devTypeId,b.Remark,c.CompSN,c.compTypeId,c.Remark AS cmpRK \
FROM t_machineinfo A LEFT JOIN t_deviceinfo B ON B.MacId = A.id \
LEFT JOIN t_componentinfo C ON B.id = C.devId ORDER BY a.id ) AS TMP \
GROUP BY MacSN ");
mysql_query(&mydbcon,strSql);//这次返回时0了,耶~
 




呵呵~今天又解决一个问题,五一准备相亲去……0.0~