【问题标题】:Query the row which with the maximum value on "cont" column查询“cont”列中最大值的行
【发布时间】:2011-04-14 05:08:11
【问题描述】:

嘿,我正在尝试查询 cont 列中具有最大值的单词。例如:如果单词 "test1" 在 cont 中的值为 5,而 "test2" 的值为 2,则 "test1" 将显示在第一个位置。明白了吗?

所以。我试图这样做,但它返回以下错误:

09-18 22:24:04.072: ERROR/AndroidRuntime(435): Caused by: android.database.sqlite.SQLiteException: 滥用聚合函数 MAX(): ,编译时:SELECT word FROM words WHERE MAX(cont ) 按顺序排序

这是我的方法:

public List<String> selectMaxCont(){
   List<String> list = new ArrayList<String>();
   Cursor cursor = this.db.query(TABLE_NAME, new String[]{"word"}, "MAX(cont)", null, null, null, "cont desc");
   if(cursor.moveToFirst()){
       do{
   list.add(cursor.getString(0));
       }while(cursor.moveToNext());
   }
   if(cursor != null && !cursor.isClosed()){
       cursor.close();
   }
   return list;

}

【问题讨论】:

    标签: android sql sqlite


    【解决方案1】:

    我认为您正在尝试按列降序排序cont:

    在这种情况下,试试这个:

    select * from words order by cont desc;
    

    你不应该需要 max() 来按列排序。

    仅供参考:

    max()是一个聚合函数,需要选择。

    select max(column) from table
    

    只返回一个值。

    select * from TABLE where COLUMN < (select max(COLUMN) from TABLE);
    

    返回 COLUMN 中的值小于该列中的最大值的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2011-09-18
      • 2014-11-23
      相关资源
      最近更新 更多