2

我的日期列是整数类型:

myDate.getTime() //long saved in db

在我研究如何最好地对即将到来的生日进行排序时,我发现了这一点: MySQL query to sort of tomorrows based on current date

我试图将解决方案翻译成 SQLite 语法,但没有运气。有人想把我推向正确的方向吗?

例如,我什至无法获得时间戳的月份:

strftime('%m', birthday) //unexpected value (dec = 1, jan = 1, jan = 6)
strftime('%m', 'now') // as expected (mar = 3)

任何帮助表示赞赏。

更新 好的,现在我真的弄得一团糟:

cur = db.rawQuery("SELECT "
                        + "(strftime('%Y','now') - strftime('%Y',datetime(("+BIRTHDATE+"/1000), 'unixepoch', 'localtime'))) AS age,"
                        + "date((" + BIRTHDATE + "/1000),'+' || age || ' year') AS currbirthday,"
                        + "date((" + BIRTHDATE + "/1000),'+' || age+1 || ' year') AS nextbirthday"
                    + " FROM " + TABLE
                    + " ORDER BY CASE" 
                        + " WHEN currbirthday < date('now') THEN nextbirthday"
                        + " ELSE  currbirthday" 
                    + " END", null); 

错误:“没有这样的列年龄”

好的,好吧,我尝试将计算“年龄”的整个表达式放在 concat 中,但结果很奇怪(年龄存储了正确的值):

Log.d("bdate", contact.getFirstname() + ": currbday=" + cur.getString(1));
Log.d("bdate", contact.getFirstname() + ": nextbday=" + cur.getString(2));

萨曼莎:currbday=-5705--6--29

萨曼莎:下一个生日=-5704--6--29

有什么建议么?

4

2 回答 2

1

如果有人想知道,这是我的解决方案:

cur = db.rawQuery("SELECT "
                        + "date(strftime('%Y', 'now','localtime')||strftime('-%m-%d', datetime(("+BIRTHDATE+"/1000), 'unixepoch', 'localtime'))) as currbirthday,"
                        + "date(strftime('%Y', 'now','localtime')||strftime('-%m-%d', datetime(("+BIRTHDATE+"/1000), 'unixepoch', 'localtime')),'+1 year') as nextbirthday"
                    + " FROM " + TABLE
                    + " ORDER BY CASE" 
                        + " WHEN currbirthday < date('now') THEN nextbirthday"
                        + " ELSE  currbirthday" 
                    + " END", null); 
于 2013-04-02T09:41:03.810 回答
0

我找到了一个采用不同方法的解决方案,它适用于我认为的任何极端情况。它根据一年中的某一天计算剩余天数,如果生日落在下一年,则更正结果:

SELECT
    *, 
    strftime('%j', birthday) - strftime('%j', 'now') AS days_remaining
FROM
    person
WHERE :n_days >= CASE
    WHEN days_remaining >= 0 THEN days_remaining
    ELSE days_remaining + strftime('%j', strftime('%Y-12-31', 'now'))
    END
;
于 2019-04-07T10:39:08.597 回答