Mysql order by和group error

时间:2013-06-23 20:23:30

标签: mysql sql sql-order-by

如何通过此查询订购?

当我尝试将orderby添加到sql查询时,我遇到了问题。

原始查询是:

SELECT SQL_CALC_FOUND_ROWS `client_data`.userid,users.username as name, 
    users.guid, `client_data`.fname, 
    `client_data`.country,`client_data`.city, 
    DATE_FORMAT(`client_data`.startdate, '%d-%m-%Y') as startdate, 
    DATE_FORMAT(`client_data`.enddate, '%d-%m-%Y') as enddate, `client_data`.registerdate, 
    `client_data`.agents, `client_data`.phone1, `client_data`.phone2, `client_data`.email, 
    `client_data`.officeName, `client_data`.address, 
    users.password, `client_data`.private, 
    users.`machineid` 
FROM `client_data` 
join users 
    on (users.iid=`client_data`.userid) 
GROUP BY `client_data`.userid 
LIMIT 0, 25

我添加“ORDER BY'client_data'.userid”来查询,然后它不起作用。新查询:

SELECT SQL_CALC_FOUND_ROWS `client_data`.userid,users.username as name, users.guid, 
    `client_data`.fname, `client_data`.country,`client_data`.city,
    DATE_FORMAT(`client_data`.startdate, '%d-%m-%Y') as startdate, 
    DATE_FORMAT(`client_data`.enddate, '%d-%m-%Y') as enddate, 
    `client_data`.registerdate, `client_data`.agents, `client_data`.phone1, `client_data`.phone2,
    `client_data`.email, `client_data`.officeName, 
    `client_data`.address, users.password, 
    `client_data`.private, users.`machineid` 
FROM `client_data` 
join users 
    on (users.iid=`client_data`.userid) 
GROUP BY `client_data`.userid 
LIMIT 0, 25 
ORDER BY 'client_data'.userid

我收到此错误代码:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'ORDER BY'client_data'.userid'的第5行正在运行

如何通过

添加订单

1 个答案:

答案 0 :(得分:6)

在MySQL中,LIMIT位于ORDER BY子句之后,因此更改:

GROUP BY `client_data`.userid LIMIT 0, 25 ORDER BY 'client_data'.userid

要:

GROUP BY `client_data`.userid ORDER BY `client_data`.userid LIMIT 0, 25