我的查询有什么问题?

时间:2013-10-09 13:12:24

标签: php mysql sql

任何人都可以帮我弄清楚为什么我在这个查询中出现SQL错误:

SELECT * FROM articles 
       LIMIT $offset, $rowsperpage 
       ORDER BY article_timestamp DESC

非常感谢!

3 个答案:

答案 0 :(得分:1)

LIMIT子句放在查询的末尾。

SELECT * FROM articles
ORDER BY article_timestamp DESC
LIMIT $offset, $rowsperpage


您必须遵循 MySQL的语法 ..

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

更多信息: - MySQL SELECT Manual

答案 1 :(得分:1)

限制应该结束。它应先订购然后获得有限的行

SELECT * FROM articles 
ORDER BY article_timestamp DESC
LIMIT $offset, $rowsperpage

答案 2 :(得分:0)

试试这个:

SELECT * 
FROM   ARTICLES 
ORDER  BY ARTICLE_TIMESTAMP DESC 
LIMIT  $OFFSET, $ROWSPERPAGE