如何在sql中插入时替换NULL值

时间:2016-02-04 09:38:33

标签: mysql sql

我的表看起来像这样

+--------+--------+--------------+--------------+
| CPI_id | Weight | score_100_UB | score_100_LB |
+--------+--------+--------------+--------------+
|    1.1 |     10 |          100 |           90 |
+--------+--------+--------------+--------------+
执行插入查询时,

表应该看起来像

+--------+--------+--------------+--------------+
| CPI_id | Weight | score_100_UB | score_100_LB |
+--------+--------+--------------+--------------+
|    1.1 |     10 |          100 |           90 |
|    5.5 |     10 |         NULL |           93 |
+--------+--------+--------------+--------------+

但NULL值应替换为100。 我也试过使用触发器。我无法得到。

提前致谢

4 个答案:

答案 0 :(得分:1)

enter image description here



select E.emp_name as Employee, ISNULL( M.emp_name,'No Manager') as Manager
from Employee_Test_salary4 E
left join Employee_Test_salary4 M
on E.report_manager = M.report_manager

or

select E.emp_name as Employee, COALESCE( M.emp_name,'No Manager') as Manager
from Employee_Test_salary4 E
left join Employee_Test_salary4 M
on E.report_manager = M.report_manager

or

select E.emp_name as Employee, CASE WHEN  M.emp_name IS NULL THEN 'No Manager' ELSE M.emp_name END as Manager
from Employee_Test_salary4 E
left join Employee_Test_salary4 M
on E.report_manager = M.report_manager




enter image description here

答案 1 :(得分:0)

更改您的表格并将字段 try: CURSOR.execute(query) except (RuntimeError, TypeError, NameError): print "RuntimeError: {0}".format(RuntimeError) 设置为具有某些默认值,如下所示

score_100_UB

在此之后,每当您尝试在此列中插入NULL值时,它将被替换为100

答案 2 :(得分:0)

如果您使用的是查询,请使用 ISNULL()功能

insert into table values (CPI_id , Weight ,ISNULL(score_100_UB ,100), score_100_LB )

答案 3 :(得分:0)

对于MySQL使用:

insert into table values (CPI_id , Weight ,IFNULL(score_100_UB ,100), score_100_LB )

或:

insert into table values (CPI_id , Weight ,COALESCE(score_100_UB ,100), score_100_LB )

SQL Server:

insert into table values (CPI_id , Weight ,ISNULL(score_100_UB ,100), score_100_LB )

甲骨文:

insert into table values (CPI_id , Weight ,NVL(score_100_UB ,100), score_100_LB )