如何在表中找到下一个max(id)

时间:2016-05-10 06:42:44

标签: mysql

让我们说

我的表格包含

Id Status
1   0
2   1
3   0
4   0
5   1
6   0

我需要输出

Id   Status
5      1

我尝试过Max(id),但它输出为

id   status
 6      0

7 个答案:

答案 0 :(得分:1)

我只能想,你想知道Status = 1的那些条目的最大id,对吧?然后使用

select max(id) from mytable where Status=1

答案 1 :(得分:0)

试试这个:

SELECT id, status
FROM myTable ORDER BY `id` DESC LIMIT 1 , 1

我假设您正在寻找第二高的Id记录值。

答案 2 :(得分:0)

我想你想要max(id),其中Status = 1?

然后使用select max(id) from table where Status=1

答案 3 :(得分:0)

由于您希望从两列获得绝对最大值,请使用:

SELECT GREATEST(MAX(id), MAX(status));

答案 4 :(得分:0)

试试这个

access token

答案 5 :(得分:0)

你的问题有点不清楚你真正想要的是什么。从您的示例中,我猜您想要最大状态的最大ID。

这是每个状态的最大ID:

select max(id), status from table
group by status;

这将产生5,1和6,0。

然后您可以过滤掉您不需要的内容,例如

select * from (
    select max(id), status from t
    group by status
) maxidperstatus
where maxidperstatus.status = (select max(status) from t);

答案 6 :(得分:0)

我假设你想要状态最大的Id 试试这个

select max(status),id from table group by id order by max(status) DESC
相关问题