如何查找特定列具有最高值的行

发布于 2024-09-11 10:47:07 字数 196 浏览 5 评论 0原文

例如,该表具有 MYINDEX 和 NAME 列。

MYINDEX | NAME
=================
1       | BOB
2       | BOB
3       | CHARLES

如何找到特定 NAME 具有最高 MYINDEX 的行?例如,我想查找名称“BOB”的 ROW-2。

For example, the table has columns MYINDEX and NAME.

MYINDEX | NAME
=================
1       | BOB
2       | BOB
3       | CHARLES

Ho do I find row with highest MYINDEX for specific NAME? E.g. I want to find ROW-2 for name "BOB".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽清梦 2024-09-18 10:47:08

如果您想查看 name = 'Bob' 的最高索引,请使用:

SELECT MAX(MYINDEX) AS [MaxIndex]
FROM myTable
WHERE Name = 'Bob'

If you wanted to see the highest index for name = 'Bob', use:

SELECT MAX(MYINDEX) AS [MaxIndex]
FROM myTable
WHERE Name = 'Bob'
不奢求什么 2024-09-18 10:47:08

如果你想跳过内连接,你可以这样做:

SELECT * FROM table WHERE NAME = 'BOB' ORDER BY MYINDEX DESC LIMIT 1;

If you want to skip the inner join, you could do:

SELECT * FROM table WHERE NAME = 'BOB' ORDER BY MYINDEX DESC LIMIT 1;
朱染 2024-09-18 10:47:08

使用

FROM TABLE SELECT MAX(MYINDEX), NAME GROUP BY NAME 

Use

FROM TABLE SELECT MAX(MYINDEX), NAME GROUP BY NAME 
勿挽旧人 2024-09-18 10:47:07

SELECT Max(MYINDEX) FROM table WHERE NAME = [insertNameHere]

编辑:获取整行:

Select * //never do this really
From Table
Where MYINDEX = (Select Max(MYINDEX) From Table Where Name = [InsertNameHere]

SELECT Max(MYINDEX) FROM table WHERE NAME = [insertNameHere]

EDIT: to get the whole row:

Select * //never do this really
From Table
Where MYINDEX = (Select Max(MYINDEX) From Table Where Name = [InsertNameHere]
蓝海似她心 2024-09-18 10:47:07

有几种方法可以解决这个问题。我假设您可能希望从该行中获得其他列,否则正如其他人所说,只需名称 MAX(my_index) ... GROUP BY name 即可。以下是几个示例:

SELECT
    MT.name,
    MT.my_index
FROM
(
    SELECT
        name,
        MAX(my_index) AS max_my_index
    FROM
        My_Table
    GROUP BY
        name
) SQ
INNER JOIN My_Table MT ON
    MT.name = SQ.name AND
    MT.my_index = SQ.max_my_index

另一种可能的解决方案:

SELECT
    MT1.name,
    MT1.my_index
FROM
    My_Table MT1
WHERE
    NOT EXISTS
    (
        SELECT *
        FROM
            My_Table MT2
        WHERE
            MT2.name = MT1.name AND
            MT2.my_index > MT1.my_index
    )

There are several ways to tackle this one. I'm assuming that there may be other columns that you want from the row, otherwise as others have said, simply name, MAX(my_index) ... GROUP BY name will work. Here are a couple of examples:

SELECT
    MT.name,
    MT.my_index
FROM
(
    SELECT
        name,
        MAX(my_index) AS max_my_index
    FROM
        My_Table
    GROUP BY
        name
) SQ
INNER JOIN My_Table MT ON
    MT.name = SQ.name AND
    MT.my_index = SQ.max_my_index

Another possible solution:

SELECT
    MT1.name,
    MT1.my_index
FROM
    My_Table MT1
WHERE
    NOT EXISTS
    (
        SELECT *
        FROM
            My_Table MT2
        WHERE
            MT2.name = MT1.name AND
            MT2.my_index > MT1.my_index
    )
吲‖鸣 2024-09-18 10:47:07
SELECT MAX(MYINDEX) FROM table
WHERE NAME = 'BOB'

对于整行,执行以下操作:

SELECT * FROM table
WHERE NAME = 'BOB'
AND MyIndex = (SELECT Max(MYINDEX) from table WHERE NAME = 'BOB')
SELECT MAX(MYINDEX) FROM table
WHERE NAME = 'BOB'

For the whole row, do:

SELECT * FROM table
WHERE NAME = 'BOB'
AND MyIndex = (SELECT Max(MYINDEX) from table WHERE NAME = 'BOB')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文