我有这个SQL
查询,其中列出了本月付款的人员,以及谁没有:
SELECT main.*
FROM (SELECT t1.client_name, t1.total_debts, t2.client_id, sum(payment) payments , t2.date_now ,
CASE MONTH(t2.date_now)
WHEN MONTH(CURDATE()) THEN 'Paid'
ELSE 'Not Paid'
END current_month
FROM client_debts t1 INNER JOIN client_details t2 ON t1.id = t2.client_id
GROUP BY t2.client_id) main
WHERE main.total_debts<> main.payments;
所以我得到了这个结果:
正如我们在图片中看到的那样,我有同样的客户,本月支付一件物品,但他没有为另一件物品付款,所以实际上,他来到我的商店并给了我钱,所以我需要将其名称从列表Not Paid
中删除,并将其保留在Paid
所在的位置。
如何在PHP中运行此查询,以便将其放在表格中进行显示。 I tried the answer here in this link
$sql = "SELECT main.*
FROM (SELECT t1.client_name, t1.total_debts, t2.client_id, sum(payment) payments , t2.date_now ,
CASE MONTH(t2.date_now)
WHEN MONTH(CURDATE()) THEN 'Paid'
ELSE 'Not Paid'
END current_month
FROM client_debts t1 INNER JOIN client_details t2 ON t1.id = t2.client_id
GROUP BY t2.client_id) main
WHERE main.total_debts<> main.payments;";
$stmt = $conn->prepare($sql);
$exec = $stmt->execute();
$res = $stmt->fetchAll();
然后HTML:
<div class="col-lg-6">
<table>
<?php foreach($res as $row){ ?>
<tr>
<td><?php echo $row['t1.client_name']; ?></td>
</tr>
<?php } ?>
</table>
</div>
但它一直告诉我:
注意:未定义的索引:C:\ wamp \ www ...
中的id
答案 0 :(得分:0)
您可以通过在and
子句中添加where
条件来处理您的SQL问题。这假设所有行都有权获得该付款期限。如果数据跨越多个月,那么我们需要另一个查询。
SELECT main.*
FROM (SELECT t1.client_name, t1.total_debts, t2.client_id, sum(payment) payments , t2.date_now ,
CASE MONTH(t2.date_now)
WHEN MONTH(CURDATE()) THEN 'Paid'
ELSE 'Not Paid'
END current_month
FROM client_debts t1 INNER JOIN client_details t2 ON t1.id = t2.client_id
GROUP BY t2.client_id
) main
WHERE main.total_debts<> main.payments
and current_month='Not Paid'
and client_name not in
(select client_name from main
where current_month='Paid'
);