Selecting all records using SQL LIMIT and OFFSET query

后端 未结 7 572
一向
一向 2020-12-08 20:09

I wonder if there is a way to accomplish:

SELECT * FROM table

by using LIMIT and OFFSET like so:

         


        
相关标签:
7条回答
  • 2020-12-08 20:15

    From the MySQL documentation:

    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;

    So getting all rows might look as follows:

    SELECT * FROM tbl LIMIT 0,18446744073709551615;
    
    0 讨论(0)
  • 2020-12-08 20:16

    As the record will grow up, use mysql_num_rows to dynamically find total amount of records, instead of using some large number.

    $cektotalrec=mysql_query("SELECT * FROM TABLE");
    $numoffset=mysql_num_rows($cektotalrec); 
    $numlimit="0";
    

    then:

    $final="SELECT * FROM table ".$numlimit.", ".$numoffset"";
    
    0 讨论(0)
  • 2020-12-08 20:19

    Yes, it is possible by providing NULL:

    SELECT * FROM tab LIMIT NULL OFFSET NULL
    

    db<>fiddle PostgreSQL demo

    7.6. LIMIT and OFFSET

    LIMIT ALL is the same as omitting the LIMIT clause, as is LIMIT with a NULL argument.


    Snowflake LIMIT / FETCH

    The values NULL, empty string (''), and $$$$ are also accepted and are treated as “unlimited”; this is useful primarily for connectors and drivers (such as the JDBC driver) if they receive an incomplete parameter list when dynamically binding parameters to a statement.

    SELECT * FROM demo1 ORDER BY i LIMIT NULL OFFSET NULL;
    
    SELECT * FROM demo1 ORDER BY i LIMIT '' OFFSET '';
    
    SELECT * FROM demo1 ORDER BY i LIMIT $$$$ OFFSET $$$$; 
    
    0 讨论(0)
  • 2020-12-08 20:22

    Maybe not the cleanest solution but setting limit to a very high number could work. Offset needs to be 0.

    Why not use a IF statement where you add the limit and offset to the query as a statement is true?

    0 讨论(0)
  • 2020-12-08 20:24

    This may not be the best way to do it, but its the first that comes to mind...

    SELECT * FROM myTable LIMIT 0,1000000

    Replace 1000000 with some adequately large number that you know will always be larger than the total number of records in the table.

    0 讨论(0)
  • 2020-12-08 20:29

    I used this code in nodeJS with MySQL and it's run well, It's may help you. Why you use it?

    1. It's reliable because it's a string that will append with query.
    2. If you want to set limit then you can put the limitation with the variable otherwise pass 0 with variable.

      var noOfGroupShow=0;  //0: all, rest according to number   
      if (noOfGroupShow == 0) {
          noOfGroupShow = '';
      } 
      else {
          noOfGroupShow = ' LIMIT 0, '+noOfGroupShow;
      }
      var sqlGetUser = "SELECT `user_name`,`first_name`,`last_name`,`image`,`latitude`, `longitude`,`phone`,`gender`,`country`,`status_message`,`dob` as user_date_of_birth FROM `tb_user` WHERE `user_id`=?"+noOfGroupShow;
      
    0 讨论(0)
提交回复
热议问题