Php 当使用大小写时,如果在左联接中找不到匹配行,则忽略列
我想拿一份医院账单的收款报告。有一张表格记录了病人支付的预付款。并不是所有的病人都预付 这就是我现在使用的查询Php 当使用大小写时,如果在左联接中找不到匹配行,则忽略列,php,mysql,sql,Php,Mysql,Sql,我想拿一份医院账单的收款报告。有一张表格记录了病人支付的预付款。并不是所有的病人都预付 这就是我现在使用的查询 "SELECT SUM( CASE WHEN bill_type = 3 THEN b.payable_amount + w.balance ELSE 0 END) AS 'amount' FROM bills b LEFT JOIN ip_patients ip on ip.id = b.ip_id LEFT JOIN advance
"SELECT SUM(
CASE
WHEN bill_type = 3 THEN b.payable_amount + w.balance
ELSE 0
END) AS 'amount'
FROM bills b
LEFT JOIN ip_patients ip on ip.id = b.ip_id
LEFT JOIN advance w on w.id = (SELECT x.id FROM advance x WHERE x.ip_id = ip.id ORDER BY x.created_at DESC LIMIT 1)
WHERE b.doctor_id = '$d->id' AND CAST(b.created_at AS DATE) >= '$date1' AND CAST(b.created_at AS DATE) <= '$date2' AND b.is_cancelled = 0 AND b.is_deleted = 0"
如果患者未支付预付款,则预付款表中不会插入任何行。上述查询的结果是,当预付病人表中没有行时,他的付款将被完全忽略,即账单表中的值不会添加到总和中。如果将null添加到not null,则结果为null。比如说
MariaDB [sandbox]> set @a = null;
Query OK, 0 rows affected (0.00 sec)
MariaDB [sandbox]> set @b = 100;
Query OK, 0 rows affected (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select @a+@b amt;
+------+
| amt |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
您可以使用ifnull来处理这个问题
set @a = null;
set @b = 100;
select ifnull(@a,0) + ifnull(@b,0) amt;
+-----+
| amt |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)
如果将null添加到not null,则结果为null。比如说
MariaDB [sandbox]> set @a = null;
Query OK, 0 rows affected (0.00 sec)
MariaDB [sandbox]> set @b = 100;
Query OK, 0 rows affected (0.00 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> select @a+@b amt;
+------+
| amt |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
您可以使用ifnull来处理这个问题
set @a = null;
set @b = 100;
select ifnull(@a,0) + ifnull(@b,0) amt;
+-----+
| amt |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)