在sql查询中按问题排序会给出语法错误

时间:2013-06-08 07:29:47

标签: mysql

我在这里有这个问题:

$query_pag_data = "SELECT 
matching.date,
matching.points,
matching.time,
matching.location,
matching.epos_id,
matching.time_added,
rbpos_epos.epos_id,
rbpos_epos.location
FROM
matching
LEFT JOIN
rbpos_epos ON matching.epos_id = rbpos_epos.epos_id
WHERE
matching.user_id = ".$id_user." LIMIT $start, $per_page ORDER BY matching.time_added DESC";

我在ORDER BY部分出错,出了什么问题,我应该把它放在哪里? ...

2 个答案:

答案 0 :(得分:1)

您已经交换了ORDER BY和LIMIT子句的顺序。首先是ORDER BY子句,然后是LIMIT子句。这应该有效:

$query_pag_data = "SELECT 
matching.date,
matching.points,
matching.time,
matching.location,
matching.epos_id,
matching.time_added,
rbpos_epos.epos_id,
rbpos_epos.location
FROM
matching
LEFT JOIN
rbpos_epos ON matching.epos_id = rbpos_epos.epos_id
WHERE
matching.user_id = ".$id_user." ORDER BY matching.time_added DESC LIMIT ".$start.", ".$per_page;

答案 1 :(得分:0)

限制总是在结果收集结束时应用,因此在按顺序排列后。

鉴于你的所有条款,处理顺序将是

  • FROM
  • WHERE
  • 选择
  • ORDER BY
  • LIMIT

因此,您将获得最接近的记录< = publishedOn匹配WHERE子句中的所有条件。

@credit:RichardTheKiwi