GQL SELECT by date

2019-02-24 21:00发布

I have this model:

class Entry(db.Model):
    title = db.StringProperty()
    url = db.URLProperty()
    date = db.DateTimeProperty(auto_now=True)
    image = db.URLProperty()
    weight = db.IntegerProperty()
    category = db.StringProperty()
    desc = db.TextProperty()

I have lots of entries each day, how do I SELECT only today's entries by using GGL ? since query like this does not return any results ( but I know that there is results ):

SELECT * FROM Entry WHERE category = 'news' and date = '2012-03-12' ORDER BY weight DESC

2条回答
孤傲高冷的网名
2楼-- · 2019-02-24 21:36

If anyone need to looking for a datetime, it works for me in GQL:

select * from tableName where recordDate >= DATETIME('2018-10-07T00:56:36z')

I hope it help to anyone

查看更多
混吃等死
3楼-- · 2019-02-24 21:40

It's not saved as a string, but as a datetime like object.

The right-hand side of a comparison can be one of the following:

  • a datetime, date, or time literal, with either numeric values or a string representation, in the following forms:

    DATETIME(year, month, day, hour, minute, second)
    DATETIME('YYYY-MM-DD HH:MM:SS')
    DATE(year, month, day)
    DATE('YYYY-MM-DD')
    TIME(hour, minute, second)
    TIME('HH:MM:SS')

http://code.google.com/appengine/docs/python/datastore/gqlreference.html

So in your example, use either of these.

date = DATE('2012-03-12')
date = DATE(2012,03,12)

For datetime the time is by default set to 00:00, so equality comparison will fail therefore you must use > to compare

SELECT * FROM Entry WHERE date > DATETIME(yyyy,mm,dd)
查看更多
登录 后发表回答