I have been using PHP for a project over the past few days. The project involves MySQL operations. When optimizing the query statement, we encountered the following problem: how to return several rows of data in a table.
My initial idea was to use the php for loop to execute SQL statements multiple times. But then I thought it would be nice to do this when there is not much data. If there is more data, I don't know how much resources will be consumed when I query these times.
After trying to break your head, I finally found a solution. Use the count () function and "<=" to calculate the number. The specific idea is as follows. First, use the select statement to query the unique id value of the row of data, and then use
The Code is as follows: |
Copy code |
Select count (id) from table where id <= xxx. |
The SQL statement is like this:
SQL code
The Code is as follows: |
Copy code |
Select count (id) from table where id <= (select id from table where name = 'ku _ Andrew ') |
The table must have an id table segment, and the value of the next row is greater than that of the previous row.