从sql表中查找最大id [英] Find the maximum id from sql table

查看:240
本文介绍了从sql表中查找最大id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

id姓名

1 xxxx

2 wwww

3 uuuu

4 iiii

5 oooo

6 hhhh

7 gggg

8 nnnn

9 zzzz

10 yyyy

11 ssss

12 aaaa



我的数据是这样的。在这里,我想找到最大ID。我使用查询从表名中选择max(id)。但它显示10是最大id。我怎么能找到最大id。

id Name
1 xxxx
2 wwww
3 uuuu
4 iiii
5 oooo
6 hhhh
7 gggg
8 nnnn
9 zzzz
10 yyyy
11 ssss
12 aaaa

my data is like that. Here I want to find the maximum id. I use the query "select max(id) from tablename. but it show 10 is maximum id. how I can find max id.

推荐答案

不可能...



如果id被定义为numeric(int),它必须返回12,但是如果id是string(nvarchar),它必须返回9(10在字母短的1之后)



为了确保它的行为与数字相同,你必须将id声明为number(int)...
Impossible...

If id defined as numeric (int) it must return 12, however if id is string (nvarchar) it must return 9 (10 comes after 1 in an alphabetic short)

To ensure that it will behave like numbers do, you must declare id as number (int)...


Id不是数字,这就是结果出错的原因。如果是数字,那么结果将是12.因此,更改Id列的数据类型。或者其他方式,您可以转换Id列的数据类型并通过这种方式获取Max值。 br />
Id is not numeric thats why result comes wrong . If it is numeric then result will be 12 . so,change the datatype of Id column . or other way , you can Convert the datatype of Id column and get the Max value by this way.
select max(convert(int,id)) from tbl


SELECT MAX(id) FROM TableName



如果表中包含您发布的数据,则返回12。有一些可能性为什么你可以得到另一个结果

- id的11和12不在表格中

- 你在选择中有条件从结果中删除那些id />
- 在另一个会话中将id 11和12插入到表中,并且事务尚未提交。


Will return 12 if the table contains the data you posted. There are a few possibilities why you could get another result
- id's 11 and 12 are not in the table
- you have a condition in the select which eliminates those id's from the result
- id's 11 and 12 are inserted to the table in another session and the transaction isn't committed yet.


这篇关于从sql表中查找最大id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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