【问题标题】:sqlite - query not working when the year changessqlite - 年份更改时查询不起作用
【发布时间】:2013-01-04 11:25:36
【问题描述】:

我不擅长查询,我的 strftime 函数有问题。

SELECT SUM(QUANTITY) quantity, strftime('%m', CREATED_ON) month FROM OrderItemBIT  WHERE (strftime('%m/%Y',CREATED_ON) BETWEEN '08/2012' AND '01/2013') group by month

当我运行上述查询时,我没有得到任何数据,即使这些日期范围内有数据,因为年份不同。

SELECT SUM(QUANTITY) quantity, strftime('%m', CREATED_ON) month FROM OrderItemBIT  WHERE (strftime('%m/%Y',CREATED_ON) BETWEEN '06/2012' AND '12/2012') group by month

当我运行上述查询时,我会得到日期范围之间的数据。

SELECT SUM(QUANTITY) quantity, strftime('%m', CREATED_ON) month FROM OrderItemBIT  WHERE (strftime('%m/%Y',CREATED_ON) BETWEEN '01/2013' AND '12/2013') group by month

当我运行上述查询时,我得到的数据不在该日期范围内,因为我也得到了 2012 年的数据。查询只取月份而不是年份。

谁能帮我解决这个问题!!!

【问题讨论】:

  • 你能告诉我哪些字符串属于 '08/2012' 和 '01/2013' 之间吗?
  • 好吧,'12/2012' > '01/2013''12/2012' < '12/2013'。这就是为什么您不应该使用不是Ymd 格式的字符串进行日期比较。
  • @HamletHakobyan NSString *stringdF = [NSString stringWithFormat:@"%@/%ld", fromMonthString, fromYear];NSString *stringdT = [NSString stringWithFormat:@"%@/%ld", toMonthString , toYear];

标签: sqlite strftime


【解决方案1】:

试试这个:

SELECT SUM(QUANTITY) quantity, strftime('%m', CREATED_ON) month
FROM OrderItemBIT
WHERE (strftime('%Y%m',CREATED_ON) BETWEEN '201208' AND '201301')
GROUP BY month

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    相关资源
    最近更新 更多