Home > Database > Mysql Tutorial > How to query the next data in mysql

How to query the next data in mysql

coldplay.xixi
Release: 2020-11-12 09:35:31
Original
2694 people have browsed it

Mysql method of querying the next piece of data: Select a piece of data larger than the current ID from the news table in ascending order. The code is [table_a where id = (select min(id) from table_a where id) > {$id})].

How to query the next data in mysql

Mysql method of querying the next data:

The principle of getting the next record of the current file is the next one SQL statement, select a piece of data larger than the current ID from the news table in ascending order.

If the ID is a primary key or has an index, you can search directly:

Method 1:

1.select * from table_a where id = (select id from table_a where id < {$id} order by id desc limit 1);   
2.select * from table_a where id = (select id from table_a where id > {$id} order by id asc limit 1);
Copy after login

Method 2:

1.select * from table_a where id = (select max(id) from table_a where id < {$id});   
2.select * from table_a where id = (select min(id) from table_a where id > {$id});
Copy after login

More related Free learning recommendations: mysql tutorial(video)

The above is the detailed content of How to query the next data in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template