tags:

views:

157

answers:

2

I see ORDER BY addedOn DESC LIMIT in Mysql command in our apps i don't know Means of ORDER BY addedOn DESC LIMIT so what is ORDER BY addedOn DESC LIMIT

+2  A: 

It means the results will be ordered by the the column named addedOn in descending order and the number of rows returned will be limited to the number that appears after the word LIMIT.

Asaph
+1 you beat me to it by 3 seconds :-)
Jim Garrison
+1 apt precise answer
Richie
+2  A: 

This is ordering by addedOn in descending order and then limiting the results by the number of rows after the word LIMIT.

The definition of limit is from here

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5;     # Retrieve first 5 rows

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count. For prepared statements, you can use placeholders (supported as of MySQL version 5.0.7). The following statements will return one row from the tbl table:

SET @a=1;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?';
EXECUTE STMT USING @a;

The following statements will return the second to sixth row from the tbl table:

SET @skip=1; SET @numrows=5;
PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?';
EXECUTE STMT USING @skip, @numrows;

For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

If LIMIT occurs within a subquery and also is applied in the outer query, the outermost LIMIT takes precedence. For example, the following statement produces two rows, not one:

(SELECT ... LIMIT 1) LIMIT 2;
Dave Barker
you say for "SELECT * FROM tbl LIMIT 5; " for # Retrieve first 5 rowsHow to retrive last 5 rows
To retrieve the last rows rather than first rows, reverse the order by clause. ie rather than ordering by ascending, order by descending.
Dave Barker