1

I want to achieve the below 2 scenarios in a single query. (Note- This is just for reference, the actual query is different)

 1. SELECT * FROM CUSTOMER.CUSTOMER LIMIT :startingRow, rowsCount; //WITH LIMIT
 2. SELECT * FROM CUSTOMER.CUSTOMER; // NO LIMIT

Is it possible to write a single conditional query for this? If I pass starting row and rows count params it should go for 1st condition and if no input params are passed, it should give me all records from a table.

6
  • What programming language are you using here or else how are you binding the values? Commented Dec 22, 2022 at 8:22
  • What is MySQL version? show output for SELECT VERSION();. Commented Dec 22, 2022 at 8:26
  • This query just for example, I am selecting required columns in my actual query. And I am writing node JS service to read data from MYSQL database data and MYSQL version is 5.7.12-log. So, while executing query from service, there will be 2 scenarios- 1. I will pass limit params to get limited records. 2. I wont pass limit params to get all data Commented Dec 22, 2022 at 9:04
  • 1
    In this case I would handle this question in js: if the two parameters are supplied, then execute the sql statement that has limit clause. If the two parameters are not supplied, then execute the query without the limit clause. Commented Dec 22, 2022 at 9:16
  • What if you write both queries and check the condition if the parameters are passed Commented Dec 22, 2022 at 9:37

1 Answer 1

3

The MySQL manual gives a tip for this:

https://dev.mysql.com/doc/refman/en/select.html

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;

The very large value used in this example is 264-1, or the greatest value of BIGINT UNSIGNED. There are certainly a smaller number of rows in your table.

In your case, you could use 0 as the default offset and a very large value like that as the default limit.

Speaking for myself, I would just run two different queries. One with a LIMIT clause, and the other with no LIMIT clause. Use some kind of if/then/else structure in your client code to determine which query to run, based on whether the function has specified the limit parameters or not.

Sign up to request clarification or add additional context in comments.

2 Comments

In an InnoDB (clustered index table) will an unordered SELECT return the rows in a default ordering according to the index tree ordering, or do we need to explicitly add an ORDER BY clause for the offset?
@TheImpaler, My understanding is that InnoDB returns rows reliably in the order of the index it reads the rows from. In the example above, I would expect it to read in clustered index order. EXPLAIN should verify the index used by a given query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.