我正在使用greendao或sqlite,我的模型(用户)有一个DOB列,用于存储用户的生日。如何查找在一系列日期之间有生日的用户?
我在mysql中使用以下查询但是找不到使用sqlite执行此操作的方法:
SELECT *
FROM user
WHERE datediff( MAKEDATE( YEAR(NOW()) , DAYOFYEAR(dob) ) , NOW())
BETWEEN 0 AND 7
OR datediff( MAKEDATE( YEAR(NOW())+1 , DAYOFYEAR(dob) ) , NOW())
BETWEEN 0 AND 7
OR datediff( MAKEDATE( YEAR(NOW())-1, DAYOFYEAR(dob) ), NOW())
BETWEEN 0 AND 7;
答案 0 :(得分:1)
与SQLite文档一样,SQLite中没有日期格式(http://www.sqlite.org/datatype3.html)。
因此,greenDAO使用时间戳来保存日期。也许您正在寻找这种查询:
Date startRangeDate = new Date();
Date endRangeDate = new Date();
DeveloperDao targetDao = daoSession.getDeveloperDao();
QueryBuilder<Developer> qb = targetDao.queryBuilder();
qb.where(Properties.Birthday.between(startRangeDate.getTime(), endRangeDate.getTime()));
答案 1 :(得分:0)
经过多次尝试,我终于想出了这个解决方案。我发布这个,所以它可能在将来帮助某人:
public static List<Customer> getThisWeekCustomersByBirthday(Context context,int limit, int offset) {
String whereQuery = " julianday(strftime('%Y','now','localtime')||strftime('-%m-%d',datetime(dob/1000, 'unixepoch'),'localtime')) between julianday(strftime('%Y-%m-%d','now','weekday 0','-7 days','localtime')) and julianday(strftime('%Y-%m-%d','now','weekday 0','localtime')) "
+ "OR julianday(strftime('%Y','now','+1 year','localtime')||strftime('-%m-%d',datetime(dob/1000, 'unixepoch'),'localtime')) between julianday(strftime('%Y-%m-%d','now','weekday 0','-7 days','localtime')) and julianday(strftime('%Y-%m-%d','now','weekday 0','localtime')) "
+ "OR julianday(strftime('%Y','now','-1 year','localtime')||strftime('-%m-%d',datetime(dob/1000, 'unixepoch'),'localtime')) between julianday(strftime('%Y-%m-%d','now','weekday 0','-7 days','localtime')) and julianday(strftime('%Y-%m-%d','now','weekday 0','localtime')) ";
Query query = getCustomerDao(context).queryBuilder().where(
new WhereCondition.StringCondition(whereQuery)).limit(limit).offset(offset).build();
return query.list();
}