Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Math and COUNT(*) in LIMIT

Tags:

mysql

Is it possible to let MySQL LIMIT have an offset of the total number of rows divided in 2, so that the query would look something like this:

SELECT * FROM test LIMIT COUNT(*) / 2, 5

Where 5 is just a number.

like image 637
sigvardsen Avatar asked Sep 08 '09 20:09

sigvardsen


People also ask

Can we use count in LIMIT?

Using LIMIT you will not limit the count or sum but only the returned rows. So your query will return n rows as stated in your LIMIT clause. And since your query actually returns only one row, applying a (non-zero) limit has no effect on the results.

How do I LIMIT MySQL results?

In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count. The value of both the parameters can be zero or positive integers.

Is there a LIMIT clause in MySQL?

The MySQL LIMIT ClauseThe LIMIT clause is used to specify the number of records to return. The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

How do I LIMIT the number of rows in a MySQL table?

To limit the number of rows/records a SELECT query can fetch append your SQL Query with LIMIT modifier and the syntax of the SQL Query is given below: your_sql_query LIMIT number_of_records; If the number of records in the database for the query is less than the specified, then all those records would be fetched.


1 Answers

It is not possible.

From the documentation:

Expressions can be used at several points in SQL statements, such as in the ORDER BY or HAVING clauses of SELECT statements, in the WHERE clause of a SELECT, DELETE, or UPDATE statement, or in SET statements.

Also from the SELECT documentation:

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 nonnegative integer constants (except when using prepared statements).

You will have to do two queries--one to count, the other to limit.

like image 51
gak Avatar answered Oct 08 '22 09:10

gak