Order by和LIMIT都不能在MySQL Query中一起工作

时间:2013-09-17 06:00:38

标签: php mysql

我正在尝试按降序排序,并希望在查询中限制30

PHP代码

$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 30;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;

$query_pag_data = "SELECT * from titles LIMIT $start, $per_page ORDER BY id DESC";

错误:MySql ErrorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id DESC' at line 1

PS:我正在使用分页...因此限制了30个这样的结果

5 个答案:

答案 0 :(得分:3)

正确的语法是:

SELECT * from titles 
ORDER BY id DESC
LIMIT $start, $per_page 

查询结束时的LIMIT。

答案 1 :(得分:1)

更改职位或LIMITORDER BY,例如:

$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";

答案 2 :(得分:0)

您必须在查询结尾处设置限制以获得正确的语法:

$query_pag_data = "SELECT * from titles ORDER BY id DESC  LIMIT $start, $per_page";

答案 3 :(得分:0)

您需要在LIMIT

之前先放置ORDER BY语句

正确的语法如下:

SELECT * FROM *table_name* WHERE *condition* ORBER BY *field_name* LIMIT *limit*;

答案 4 :(得分:0)

LIMIT语句应始终位于查询的末尾。