0

朋友们。我对以下查询感到困惑。

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+"'为什么,因为我想明确给出月份编号。

请帮我。提前致谢。

4

2 回答 2

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);

关于什么 ?

于 2013-05-07T14:22:26.067 回答
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);
于 2013-05-07T14:46:58.747 回答