按字段排序SQL状态异常1064

时间:2014-02-24 21:18:35

标签: php mysql sql pdo sql-order-by

我一直在

exception 'PDOException' with message 'SQLSTATE[42000]: 
Syntax error or access violation: 1064 
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 
'(questionid, 'sfname' , 'slname' , 'school' , 'grade' , 'addr' , 'city' , 'state' at line 1' 

来自这句话:

$stmt = $db->prepare('SELECT * FROM event_responses WHERE eventid= :eventid ORDER BY userid DESC, field (questionid, \''.implode("' , '", $columns1).'\')');

我回应了里面的陈述,它对我来说很好看:

SELECT *
FROM event_responses
WHERE eventid= :eventid
ORDER BY userid DESC,
      field (questionid, 'sfname' , 'slname' , 'school' , 'grade' , 'addr' , 'city' , 'state' , 'zip' , 'semail' , 'sphone' , 'pfname' , 'plname' , 'pemail' , 'pphone' , 'noattend' , 'regid' , 'submitDate' , 'attended' , 'regmethod')

为什么会这样?

1 个答案:

答案 0 :(得分:11)

你在MySQL中遇到了一个奇怪的问题:

  

请注意

     

默认情况下,函数名和之间不能有空格   跟随它的括号。这有助于MySQL解析器区分   函数调用和对发生的表或列的引用之间的关系   与函数同名。但是,功能周围的空间   允许参数。

删除field之后的空格,使表达式为:

SELECT *
FROM event_responses
WHERE eventid = :eventid
ORDER BY userid DESC,
         field(questionid, 'sfname' , 'slname' , 'school' , 'grade' , 'addr' , 'city' , 'state' , 'zip' , 'semail' , 'sphone' , 'pfname' , 'plname' , 'pemail' , 'pphone' , 'noattend' , 'regid' , 'submitDate' , 'attended' , 'regmethod')