这是对MySQL使用MAX函数的正确方法吗?

时间:2020-11-30 18:54:27

In column_1 is just numbers from 1-10 with 10 rows. I am trying to find the biggest number and take that and eventually add it to another column.

在column_1中,数字是从1-10到10行。我想找出最大的数,然后把它加到另一列。

private ResultSet nrs;

nrs = stmt.executeQuery("SELECT MAX(column_1) FROM table");

while(nrs.next()){
     biggestNum = nrs.getInt("column_1");   
}

It keeps on throwing error 'Column "column_1" not found'.

它继续抛出error 'Column ' column_1 ' not found'。

Please help.

请帮助。

2 个解决方案

#1


1  

Replace nrs.getInt("column_1") with nrs.getInt(1)

与nrs.getInt取代nrs.getInt(“column_1”)(1)

You haven't specified a name for the value you selected, so the database will choose its own. You don't know what that name will be, so just use the column number.

您还没有为所选择的值指定名称,因此数据库将选择自己的名称。您不知道这个名称是什么,所以只需要使用列号即可。

#2


1  

Not really familiar with java but i am with sql. column_1 does not exist cause it is already aggregated. try this:

我对java不是很熟悉,但我对sql很熟悉。column_1不存在,因为它已经聚合了。试试这个:

private ResultSet nrs;

nrs = stmt.executeQuery("SELECT MAX(column_1) as maxColumn1 FROM table");

while(nrs.next()){
     biggestNum = nrs.getInt("maxColumn1");   
} 

But im pretty sure there is a better way to do this

但我很肯定有更好的办法

#1


1  

Replace nrs.getInt("column_1") with nrs.getInt(1)

与nrs.getInt取代nrs.getInt(“column_1”)(1)

You haven't specified a name for the value you selected, so the database will choose its own. You don't know what that name will be, so just use the column number.

您还没有为所选择的值指定名称,因此数据库将选择自己的名称。您不知道这个名称是什么,所以只需要使用列号即可。

#2


1  

Not really familiar with java but i am with sql. column_1 does not exist cause it is already aggregated. try this:

我对java不是很熟悉,但我对sql很熟悉。column_1不存在,因为它已经聚合了。试试这个:

private ResultSet nrs;

nrs = stmt.executeQuery("SELECT MAX(column_1) as maxColumn1 FROM table");

while(nrs.next()){
     biggestNum = nrs.getInt("maxColumn1");   
} 

But im pretty sure there is a better way to do this

但我很肯定有更好的办法