gpt4 book ai didi

c++ - SQLITE:如何在 UPDATE 语句中使用 SELECT 语句更新表?

转载 作者:行者123 更新时间:2023-11-28 00:07:56 25 4
gpt4 key购买 nike

我有以下 payments_2015 表:

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

我正在使用以下查询检查每个 ref_no 有多少个月没有支付:

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 表:

ref_no   |unpaid_count
1 |3
2 |4
3 |1
4 |2
5 |4
6 |5

现在,如果我在 1 月为 ref_no = 1 插入“PAID”,结果仍然相同。当然它会保持不变,因为我正在插入已经存在的记录而不是更新。

每当某个月针对特定“ref_no”被选中为“PAID”时,我都无法更新这些值。即:只要有变化就更新表。

更新:

我试过以下方法:

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

但是没有成功..

附注:我正在 Qt 上为一个 c++ 应用程序实现这些选择语句。我正在使用 QSqlQueryModelQTableView 上显示选择语句的结果。

最佳答案

首先,创建一个查询来计算所需的表:

SELECT refno,
(ifnull(Jan, '') != 'PAID') +
(ifnull(Feb, '') != 'PAID') +
...
(ifnull(Dec, '') != 'PAID) AS unpaid_count
FROM payments_2015;

然后您可以删除该表,而是使用动态生成这些结果的 View :

CREATE VIEW late_payments_2015 AS
SELECT ...;

关于c++ - SQLITE:如何在 UPDATE 语句中使用 SELECT 语句更新表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34483499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com