从任何表中找到最大值(非常紧急) [英] Find maximum value from any table(It's very urgent)
问题描述
执行此查询时,请帮助我简化此问题.
我有一个表,其中包含ID,名称,密码等字段.我想找到最大值,即. id我输入了以下值.
Hi,
Please help me to short out this problem while executing this query.
I have a table with fields like id,name,password. I want to find the maximum value ie. id i have following values enterd.
ID Name Password
1 abcd 111111
2 wer 222222
3 dsf 1234
4 fgty 5555
5 ghyu 77777
6 hhnj 666666
7 gggbv 99999
8 eert 8888888
9 fvgb 0000000
10 hjuik 3232323
11 erwq 45455
12 sdfgh 4565656
13 wewe 4545656
14 sdcvf 65656
15 wwwer 565656
16 fgbgh 6566
17 ertyu 6566
当执行这个查询,即.从用户中选择不同的max(id)作为ID.
但是我没有得到确切的结果.
我得到的结果ID是9,而实际结果是17.我如何找到值17?
When executing this query ie. select distinct max(id) as ids from user.
But i am not getting the exact result.
I GOT THE RESULT LIKE ID IS 9 BUT THE ACTUAL RESULT IS 17. HOW CAN I FOUND THE VALUE 17?
推荐答案
您不必为DISTINCT定义查询.只需:
You don''t have to define DISTINCT for the query. Simply:
SELECT MAX(Id) FROM User
但是,听起来您正在尝试获取最大值以自己定义下一个键值.如果这是真的,那就不要这样做!而是在创建表 [
However, it sounds that you''re trying to get the maximum in order to define the next key value by yourself. If that''s true, don''t do that! Instead use IDENTITY in CREATE TABLE[^].
Also based on your results, make sure that the data type for id is numeric (for example (int) not varchar.
不建议使用MAX()函数,因为您可能会得到其他一些结果身份值不是您的,而是来自其他用户的身份:)我认为这就是您所发生的事情.如果您仅寻找最高的身份值,则可以使用:
改为使用@@identity
以下是一些链接:
@@ IDENTITY(Transact-SQL) [ SQL服务器– @@ IDENTITY与SCOPE_IDENTITY()与IDENT_CURRENT –检索记录的最后插入的身份 [
The MAX() function is not recommended because you might get some other identity value that is not yours but from a different user :) I think thats what happened to you. If you are only looking for the highest identity value you can use:
instead use@@identity
Here are some links:
@@IDENTITY (Transact-SQL)[^]
SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record[^]
Good luck,
OI
它看起来像您的ID列不是整数,它是一个varchar,并且您尝试从varchar中查找MAX.
您需要将列的数据类型更改为int .
例如
如果我有数据p1,p2,p3,p4,p5 .... p9,p10.而我放了MAX,那么结果将是p9而不是p10原因
它将p10视为"p","1","0"作为单个字符.
it looks your ID column is not interger it''s a varchar and your try to find MAX from varchar.
you need to change datatype of column to int.
e.g.
if i have data p1,p2,p3,p4,p5....p9,p10. and i put MAX then result will be p9 not p10 cause
it consider p10 as ''p'',''1'',''0'' as an individual characters.
这篇关于从任何表中找到最大值(非常紧急)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!