为什么SQLite min()函数在此查询中不起作用?

时间:2017-04-29 11:02:12

标签: android android-sqlite

让我们说有一个价格提交表,包括rowId,shopID,goodID,价格,提交时间。我试图检索给定商店中最早的价格提交。我的查询如下:

button-primary

此查询返回0结果。虽然如果我用实际的最小(最旧)值替换部分Cursor c = database.rawQuery("SELECT * FROM price_submissions WHERE validshop = '-Kcq2pOeJA2_URuJLFKC' AND submissiondateandtime = (SELECT min(submissiondateandtime) FROM price_submissions)", null); ,它确实会返回一行。

为什么初始查询不起作用?

2 个答案:

答案 0 :(得分:2)

SELECT min(submissiondateandtime) FROM price_submissions返回不在给定商店中的最早的价格提交,因为您在文字说明中写入,但在整个表格中。你的意思可能是SELECT * FROM price_submissions AS outer WHERE validshop = '-Kcq2pOeJA2_URuJLFKC' AND submissiondateandtime = (SELECT min(submissiondateandtime) FROM price_submissions AS inner WHERE inner.validshop = outer.validshop)

答案 1 :(得分:0)

首先运行SELECT min(submissiondateandtime)FROM price_submissions并将min值存储在一个可变X中,然后在父查询中使用SELECT * FROM price_submissions WHERE validshop =' - Kcq2pOeJA2_URuJLFKC'AND submissiondateandtime = X