从sqlite数据库按月选择

时间:2013-05-07 14:18:09

标签: android sqlite

友。我对以下问题感到困惑。

c = db.rawQuery("SELECT strftime('%W', tm.txn_date) AS week, 
               sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, 
               sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense 
               FROM transmaster tm 
               INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id 
               INNER JOIN catmasters cm ON scm.catmaster_id = cm.id 
               WHERE strftime('%m', tm.txn_date) = '05' 
               GROUP BY week ORDER BY week DESC", null);

这个很好用,但是。

c = db.rawQuery("SELECT strftime('%W', tm.txn_date) AS week, 
               sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, 
               sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense 
               FROM transmaster tm 
               INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id 
               INNER JOIN catmasters cm ON scm.catmaster_id = cm.id 
               WHERE strftime('%m', tm.txn_date) = '"+05+"' 
               GROUP BY week ORDER BY week DESC", null);

这个没用。朋友有什么问题。

我只是将WHERE strftime('%m', tm.txn_date) = '05'这一行更改为WHERE strftime('%m', tm.txn_date) = '"+05+"'为什么因为我想明确给出月号。

请帮帮我。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

c = db.rawQuery("SELECT strftime('%W', tm.txn_date) AS week, 
               sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, 
               sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense 
               FROM transmaster tm 
               INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id 
               INNER JOIN catmasters cm ON scm.catmaster_id = cm.id 
               WHERE strftime('%m', tm.txn_date) = '0"+5+"' 
               GROUP BY week ORDER BY week DESC", null);

怎么样?

答案 1 :(得分:1)

你应该做一些更清洁的事情,并为你的月号声明一个变量:

String monthNumber = "05";
String request = "SELECT strftime('%W', tm.txn_date) AS week, " +
               "sum(case when cm.master_id = 1 then (tm.amount - tm.pre_amount) else 0 end) AS Income, " +
               "sum(case when cm.master_id = 2 then (tm.pre_amount - tm.amount) else 0 end) AS Expense " +
               "FROM transmaster tm " +
               "INNER JOIN subcatmasters scm ON tm.scmaster_id = scm.id " +
               "INNER JOIN catmasters cm ON scm.catmaster_id = cm.id " +
               "WHERE strftime('%m', tm.txn_date) = '" + monthNumber + "' " +
               "GROUP BY week ORDER BY week DESC";
c = db.rawQuery(request, null);