MySQL查询通过跳过值来选择列中的第n个最大值

要获得列中的第n个最大值,可以使用LIMIT OFFSET。在此,OFFSET用于跳过值。让我们首先创建一个表-

create table DemoTable

(

   Value int

) ;

使用插入命令在表中插入一些记录-

insert into DemoTable values(100);

insert into DemoTable values(140);

insert into DemoTable values(90);

insert into DemoTable values(80);

insert into DemoTable values(89);

insert into DemoTable values(98);

insert into DemoTable values(58);

使用select语句显示表中的所有记录-

select *from DemoTable;

这将产生以下输出-

+-------+

| Value |

+-------+

|   100 |

|   140 |

|    90 |

|    80 |

|    89 |

|    98 |

|    58 |

+-------+

7 rows in set (0.00 sec)

以下是查询以获取列中的第n个最大值。在这里,我们使用OFFSET 3跳过3个值。在使用ORDER BY之后,相同的值使我们排在第4位-

select Value from DemoTable

   order by Value limit 1 offset 3;

这将产生以下输出-

+-------+

| Value |

+-------+

|    90 |

+-------+

1 row in set (0.00 sec)

以上是 MySQL查询通过跳过值来选择列中的第n个最大值 的全部内容, 来源链接: utcz.com/z/317126.html

回到顶部