如何选择表中具有最大 ID 的整行?

2021-11-20 00:00:00 sql mysql

我该怎么做这样的事情?

<前>SQL SELECT row FROM table WHERE id=max(id)

解决方案

您可以使用子选择:

SELECT 行发件人表哪里 id=(SELECT max(id) FROM 表)

请注意,如果max(id) 的值不唯一,则返回多行.

如果您只想要一个这样的行,请使用@MichaelMior 的答案,

SELECT row from table ORDER BY id DESC LIMIT 1

How would I do something like this?

SQL SELECT row FROM table WHERE id=max(id)

解决方案

You could use a subselect:

SELECT row 
FROM table 
WHERE id=(
    SELECT max(id) FROM table
    )

Note that if the value of max(id) is not unique, multiple rows are returned.

If you only want one such row, use @MichaelMior's answer,

SELECT row from table ORDER BY id DESC LIMIT 1

相关文章