How would I do something like this? I've been trying to figure this out for about an hour. Very frustrating. Any help would be awesome!
我该怎么做呢?我已经想了一个小时了。非常令人沮丧。任何帮助都会很棒!
6 个解决方案
#1
181
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.
注意,如果max(id)的值不是唯一的,则返回多个行。
If you only want one such row, use @MichaelMior's answer,
如果你只想要一个这样的行,使用@MichaelMior的答案,
SELECT row from table ORDER BY id DESC LIMIT 1
#2
114
You could also do
你也可以做
SELECT row FROM table ORDER BY id DESC LIMIT 1;
This will sort rows by their ID in descending order and return the first row. This is the same as returning the row with the maximum ID. This of course assumes that id
is unique among all rows. Otherwise there could be multiple rows with the maximum value for id
and you'll only get one.
这将按其ID按降序排列行并返回第一行。这与返回具有最大ID的行相同,这当然假定在所有行中ID是惟一的。否则,可能会有多个具有id的最大值的行,您只会得到一个。
#3
21
SELECT *
FROM table
WHERE id = (SELECT MAX(id) FROM TABLE)
#4
17
You can not give order by
because order by
does a "full scan" on a table.
你不能按顺序来排序,因为顺序是在表上做“全扫描”。
The following query is better:
下面的查询比较好:
SELECT * FROM table WHERE id = (SELECT MAX(id) FROM table);
#5
0
Try with this
试着用这个
SELECT top 1 id, Col2, row_number() over (order by id desc) FROM Table
#6
-1
you can also use COUNT(id) instead of MAX(id)
你也可以用COUNT(id)代替MAX(id)
SELECT * FROM table WHERE id = (SELECT count(id) FROM table)
#1
181
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.
注意,如果max(id)的值不是唯一的,则返回多个行。
If you only want one such row, use @MichaelMior's answer,
如果你只想要一个这样的行,使用@MichaelMior的答案,
SELECT row from table ORDER BY id DESC LIMIT 1
#2
114
You could also do
你也可以做
SELECT row FROM table ORDER BY id DESC LIMIT 1;
This will sort rows by their ID in descending order and return the first row. This is the same as returning the row with the maximum ID. This of course assumes that id
is unique among all rows. Otherwise there could be multiple rows with the maximum value for id
and you'll only get one.
这将按其ID按降序排列行并返回第一行。这与返回具有最大ID的行相同,这当然假定在所有行中ID是惟一的。否则,可能会有多个具有id的最大值的行,您只会得到一个。
#3
21
SELECT *
FROM table
WHERE id = (SELECT MAX(id) FROM TABLE)
#4
17
You can not give order by
because order by
does a "full scan" on a table.
你不能按顺序来排序,因为顺序是在表上做“全扫描”。
The following query is better:
下面的查询比较好:
SELECT * FROM table WHERE id = (SELECT MAX(id) FROM table);
#5
0
Try with this
试着用这个
SELECT top 1 id, Col2, row_number() over (order by id desc) FROM Table
#6
-1
you can also use COUNT(id) instead of MAX(id)
你也可以用COUNT(id)代替MAX(id)
SELECT * FROM table WHERE id = (SELECT count(id) FROM table)