0

web2pyに次のコードがあります。テーブルにあるアイテムの種類と、それぞれの数の数を取得しようとしています。

count = db.table.field1.count()
rows=db((some criteria).select(db.table.field2, count, groupby=db.table.field2)
print rows

これのプリントは次のとおりです。

table.field2, COUNT(table.field1)
4,3
6,4
9,2

ここで、count フィールドで高いものから低いものへと並べ替えたいので、結果は次のようになります。

6,4
4,3
9,2

それを行う最良の方法は何ですか?rows=rows.sort(lambda row: row.COUNT(table.field1)) はうまくいきませんでした。

4

1 に答える 1

1

Instead of row.COUNT(table.field1), use row['COUNT(table.field1)'] or just row[count] (see here).

Note, you can also have the database do the sorting using the orderby argument:

rows = db(query).select(db.table.field2, count,
                        groupby=db.table.field2, orderby=count)

And for descending order: orderby=~count

于 2013-05-13T13:41:32.337 に答える