我已经知道 如何获取一个月的数据? 使用此查询 -
SELECT SLIDER_VALUE FROM TBL_ACTIVITY_TRACKS WHERE STRFTIME('%m', TRACK_DATE_TIME, 'localtime') = STRFTIME('%m', 'now', 'localtime');
Here TRACK_DATE_TIME is in yyyy-mm-dd date format.
但我希望如果没有特定日期的数据,那么我希望用零或特定标识符代替它。
我也知道我可以通过创建 TBL_DATES 来做到这一点。其中包含所有日期,然后将其与我的表一起加入,但我不想创建新表。
我可以这样做吗?
最佳答案
您必须从某处 获取各个日期值。 如果您不想创建临时表,您可以直接在语句中将值嵌入到子查询中:
SELECT IFNULL(Slider_Value, 'none')
FROM (SELECT '2013-10-01' AS Date
UNION ALL
SELECT '2013-10-02'
UNION ALL
SELECT '2013-10-03'
UNION ALL
...
SELECT '2013-10-31'
) AS ThisMonth
LEFT JOIN Tbl_Activity_Tracks ON ThisMonth.Date = date(Track_Date_Time)
如果您不想在生成 SQL 语句的代码中枚举日期,您也可以在 SQL 本身中执行此操作,但要有足够的扭曲:
SELECT IFNULL(Slider_Value, 'none')
FROM (SELECT date('now', 'start of month') AS Date
UNION ALL
SELECT date('now', 'start of month', '+1 days')
UNION ALL
SELECT date('now', 'start of month', '+2 days')
UNION ALL
...
UNION ALL
SELECT date('now', 'start of month', '+27 days')
UNION ALL
SELECT date('now', 'start of month', '+28 days') WHERE strftime('%m', 'now', 'start of month', '+28 days') = strftime('%m', 'now')
UNION ALL
SELECT date('now', 'start of month', '+29 days') WHERE strftime('%m', 'now', 'start of month', '+29 days') = strftime('%m', 'now')
UNION ALL
SELECT date('now', 'start of month', '+30 days') WHERE strftime('%m', 'now', 'start of month', '+30 days') = strftime('%m', 'now')
) AS ThisMonth
LEFT JOIN Tbl_Activity_Tracks ON ThisMonth.Date = date(Track_Date_Time)
(对于少于 31 天的月份,最后三个 WHERE 子句省略了不再在当月的天数。)
关于android - 如何使用 sqlite 获取一个月的数据?如果没有特定日期的数据,那么我想要零代替那个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19486774/