我有以下payment_2015表:
View
我正在检查每个ref_no使用以下查询未支付多少个月:
ref-no |Jan |Feb |Mar |Apr |May |Jun
1 | |PAID |PAID | |PAID |
2 | |PAID | | | |PAID
3 |PAID | |PAID |PAID |PAID |PAID
4 |PAID |PAID | |PAID |PAID |
5 |PAID | |PAID | | |
6 | | | | | |PAID
结果是以下for(int i = 0; i < all_refNo_list.size(); i++) {
qryTxt = "INSERT INTO late_payments "
"select ref_no AS ref_no, sum(i_count) as unpaid_count from ("
"SELECT ref_no, CASE WHEN January = 'PAID' THEN 0 ELSE 1 END AS i_count, 1 AS month_no FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN February = 'PAID' THEN 0 ELSE 1 END , 2 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN March = 'PAID' THEN 0 ELSE 1 END , 3 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN April = 'PAID' THEN 0 ELSE 1 END , 4 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN May = 'PAID' THEN 0 ELSE 1 END , 5 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN June = 'PAID' THEN 0 ELSE 1 END , 6 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN July = 'PAID' THEN 0 ELSE 1 END , 7 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN August = 'PAID' THEN 0 ELSE 1 END , 8 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN September = 'PAID' THEN 0 ELSE 1 END , 9 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN October = 'PAID' THEN 0 ELSE 1 END , 10 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN November = 'PAID' THEN 0 ELSE 1 END , 11 FROM payments_" + getCurrentYear() +
" UNION SELECT ref_no, CASE WHEN December = 'PAID' THEN 0 ELSE 1 END , 12 FROM payments_" + getCurrentYear() +
") A Where ref_no = " + all_refNo_list[i];
query.prepare(qryTxt);
if (!query.exec())
{
qDebug() << "Error" << query.lastError().text();
}
}
表:
late_payments
现在,如果我插入&#39; PAID&#39;在1月为ref_no = 1,结果仍然相同。当然它会保持不变,因为我正在插入已经存在的记录而不是UPDATing。
每当检查一个月时,我都无法更新这些值,因为付费&#39;具体&#39; ref_no&#39;。即:只要有变化,就更新表格。
更新
我尝试过以下方法:
ref_no |unpaid_count
1 |3
2 |4
3 |1
4 |2
5 |4
6 |5
但没有成功..
P.S。:我正在Qt上为c ++应用程序实现这些选择语句。我使用for(int i = 0; i < getAllRefNoList().size(); i++) {
qryTxt = "UPDATE late_payments "
"SET unpaid_count = ("
"select sum(i_count) as unpaid_count from ("
"SELECT CASE WHEN January = 'PAID' THEN 0 ELSE 1 END AS i_count, 1 AS month_no FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN February = 'PAID' THEN 0 ELSE 1 END , 2 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN March = 'PAID' THEN 0 ELSE 1 END , 3 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN April = 'PAID' THEN 0 ELSE 1 END , 4 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN May = 'PAID' THEN 0 ELSE 1 END , 5 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN June = 'PAID' THEN 0 ELSE 1 END , 6 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN July = 'PAID' THEN 0 ELSE 1 END , 7 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN August = 'PAID' THEN 0 ELSE 1 END , 8 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN September = 'PAID' THEN 0 ELSE 1 END , 9 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN October = 'PAID' THEN 0 ELSE 1 END , 10 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN November = 'PAID' THEN 0 ELSE 1 END , 11 FROM payments_" + getCurrentYear() +
" UNION SELECT CASE WHEN December = 'PAID' THEN 0 ELSE 1 END , 12 FROM payments_" + getCurrentYear() +
") A WHERE ref_no = " + getAllRefNoList()[i] + ")";
query.prepare(qryTxt);
if (!query.exec())
{
qDebug() << "Error" << query.lastError().text();
}
在QTableView
上显示select语句的结果。
答案 0 :(得分:1)
select语句的更新语句为:
UPDATE A
SET COUNT = <your value>
WHERE ref_no = 1
答案 1 :(得分:1)
看起来你这里有两张桌子。一种方法是:
update b
set count = (select count from a where ref_no = 1);
这将更新b
中的所有行。您的问题并未建议将其限制为一行或少量行。
答案 2 :(得分:1)
首先,创建一个查询来计算所需的表:
SELECT refno,
(ifnull(Jan, '') != 'PAID') +
(ifnull(Feb, '') != 'PAID') +
...
(ifnull(Dec, '') != 'PAID) AS unpaid_count
FROM payments_2015;
然后您可以删除该表,而是使用动态生成这些结果的视图:
CREATE VIEW late_payments_2015 AS
SELECT ...;