在一个MySQL命令中选择最大和最小记录 [英] Selecting the Max and Min records in one MySQL command

查看:81
本文介绍了在一个MySQL命令中选择最大和最小记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够根据ID从表中选择两条记录.

I need to be able select the two records from a table based on ID.

我需要第一个和最后一个(所以最小和最大) IE浏览器.

I need the first one, and the last one (so min, and max) IE.

table: Customer
    id    name
    1     Bob
    50    Bob

有什么想法吗?谢谢

推荐答案

SELECT MIN(id), MAX(id) FROM tabla

编辑:如果您需要检索该行的值,则可以执行以下操作:

If you need to retrive the values of the row you can do this:

SELECT *
FROM   TABLA AS a, (SELECT MIN(id) AS mini,
                            MAX(id) AS maxi
                     FROM   TABLA) AS m
WHERE  m.maxi = a.id
       OR m.mini = a.id;

这篇关于在一个MySQL命令中选择最大和最小记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆