我有一张包含不同类型费用的表格,如保险费,学费,入场费等,所有这些栏目也接受空值。
我想用0值更新这些列的所有空值。我可以为每个列编写不同的更新查询,但由于所有列的条件和值都相同,我想学习在单个更新查询中执行此操作。
Table_Fees
securityFees | TuitionFees | AdmissionFees
150 500 null
null 450 120
150 null 120
如何将所有空值替换为零?
Update Table_Fees
set securityFees = 0,
TuitionFees = 0,
AdmissionFees = 0
where securityFees = null
and TuitionFees = null
and AdmissionFees = null;
或/和这两个运算符在这种情况下都没用
PS:我无法改变表的结构。我只能通过编码将0替换为空值。
答案 0 :(得分:3)
如果您使用MySQL
Update Table_Fees
set securityFees = IFNULL(securityFees,0),
TuitionFees = IFNULL(TuitionFees,0),
AdmissionFees = IFNULL(AdmissionFees,0)
where securityFees is null
or TuitionFees is null
or AdmissionFees is null;
答案 1 :(得分:1)
Update Table_Fees
set securityFees = IFNULL(securityFees,0),
TuitionFees = IFNULL(TuitionFees,0),
AdmissionFees = IFNULL(AdmissionFees,0)
// 3 individual querys
Update Table_Fees
set securityFees = 0
where securityFees is null;
Update Table_Fees
set TuitionFees = 0
where TuitionFees is null;
Update Table_Fees
set AdmissionFees = 0
where AdmissionFees is null;
如果必须在整个表格中更新,则无需在何处条件。
希望这适合你。
如果有任何问题,请告诉我
答案 2 :(得分:0)
运行此命令,您将获得所需的输出描述。
SELECT 'UPDATE yourtable SET ' + name + ' = NULL WHERE ' + name + ' = '''';'
FROM syscolumns
WHERE id = object_id('yourtable')
AND isnullable = 1;
答案 3 :(得分:0)
我已经通过CASE解决了答案
Update Table_Fees
set securityFees = CASE when securityFees = null then 0 else securityFees END,
TuitionFees = CASE when TuitionFees = null then 0 else TuitionFees END,
AdmissionFees = CASE when AdmissionFees = null then 0 else AdmissionFees END;