4ndro1d 4ndro1d - 3 months ago 12
MySQL Question

MySql combine LIMIT and ORDER BY

I want to execute the following command

SELECT * FROM `auktionen` LIMIT 0, 5 ORDER BY createdat DESC


which tells me I have an SQL syntax error. I followed this questions answer.

Whenever I delete either the LIMIT or the ORDER BY statement it will execute again:

SELECT * FROM `auktionen` ORDER BY createdat DESC
SELECT * FROM `auktionen` LIMIT 0, 5


So what exactly am I doing wrong?

Answer

gg wp. Thanks for the hint, add as answer if you want it accepted – 4ndro1d

As per OP's request.

LIMIT goes last. That was a bad answer http://stackoverflow.com/a/26041483/1415724.

Consult the manual on SELECT for yourself http://dev.mysql.com/doc/refman/5.7/en/select.html

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [MAX_STATEMENT_TIME = N]
      [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
      [PARTITION partition_list]
    [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'
        [CHARACTER SET charset_name]
        export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]