Home  >  Article  >  Database  >  How to query the next data in mysql

How to query the next data in mysql

coldplay.xixi
coldplay.xixiOriginal
2020-11-12 09:35:312629browse

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);

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});

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!

Statement:
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