英文:
Go + App Engine Datastore: How to filter out rows that are null?
问题
如何过滤掉空行?我知道找到只有空行很困难,但希望这样做会更容易。
我想要做以下操作:
q := datastore.NewQuery("MY_KIND").Filter("MY_ID !=", nil)
... 但是Filter不支持!=比较符。顺便说一下,在Datastore Viewer中使用这个GQL语法是可以正常工作的:
SELECT * FROM MY_KIND WHERE MY_ID != NULL
英文:
How do I filter out rows that are null? I know it's hard to find only-null rows, but hopefully this should be easier.
I'd like to do the following:
q := datastore.NewQuery("MY_KIND").Filter("MY_ID !=", nil)
... but Filter doesn't support the != comparator. FYI, using this GQL syntax in the Datastore Viewer works just fine:
SELECT * FROM MY_KIND WHERE MY_ID != NULL
答案1
得分: 2
你可以使用greater
过滤器,并设置适当的值(对于数字,设置为> 0;对于字符串,设置为> "")。
通常情况下,ID不能是空字符串或零。
英文:
You can use greater
filter with the appropriate value (> 0 for numbers, > "" for strings).
Typically an ID cannot be an empty string or zero.
答案2
得分: 0
这是对我有效的方法。
// 数据存储实体的空值
// 在我的情况下,DeletedAt
是 *time.Time 类型
{
"DeletedAt": NULL,
"Version": 1,
...
}
这是我用来获取 DeletedAt
为 NULL
的记录的查询语句:
datastore.NewQuery("MyKind").Filter("DeletedAt =", (*time.Time)(nil))
希望对你们中的某些人有所帮助。
英文:
Here's what works for me.
// Datastore entity with null value
// `DeletedAt` in my case is *time.Time
{
"DeletedAt": NULL,
"Version": 1,
...
}
Here's my query to get records where DeletedAt
is NULL
:
datastore.NewQuery("MyKind").Filter("DeletedAt =", (*time.Time)(nil))
Hope that this will be helpful to someone of you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论