【问题标题】:GQL SELECT SortingGQL 选择排序
【发布时间】:2012-03-20 10:53:03
【问题描述】:

有没有更简单的方法可以按重量进行选择和排序?

fetchCount = 1000
date1 = datetime.datetime.utcnow().date() 
entries = GqlQuery("SELECT * FROM Entry WHERE category = :category and date >= :datetime ORDER BY date, weight DESC", category = category, datetime = date1).fetch(fetchCount)

if entries is not None:
    # Sort entries ( lazy way for now ).
    sort = True
    while sort:
        sort = False
        for i in range(0, len(entries)-1):
            if entries[i].weight < entries[i + 1].weight:
                e = entries[i + 1]
                entries[i + 1] = entries[i]
                entries[i] = e
                sort = True

【问题讨论】:

  • 目前的方式有什么难点?
  • 因为我不想每次都这样排序,如果可能的话,我想用查询本身来做。
  • 没看懂问题,是不是第一个查询不行?
  • 是的,第一个查询有效,但它只按日期排序,根本不按重量排序……我需要按重量排序。

标签: google-app-engine google-cloud-datastore gql


【解决方案1】:

解决者:

entries = GqlQuery("SELECT * FROM Entry WHERE category = :category and date > :datetime ORDER BY date, weight DESC", category = category, datetime = date1).fetch(fetchCount)
entries = sorted(entries, key=lambda x: x.weight, reverse=True)

既然没有其他办法atm....

【讨论】:

    【解决方案2】:

    如果您使用不等式过滤器(例如 date >= :datetime),则数据存储区的限制也必须是您的第一个排序键。此外,每个查询只能在一个属性上存在不等式。因此,在您的情况下,您别无选择,只能在内存中对它们进行排序。另一个答案中的 sorted() 调用是完美的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-23
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多