[英]select id, table1.column1 + table2.column2 as total from table1 join table2. what if table2.column2 has no value?
help. 救命。 T_T
T_T
table 1 表格1
+---------------+---------+-------------------+
| allowances_id | desc_id | column1 |
+---------------+---------+-------------------+
| 1 | 1 | 64055.35594866848 |
| 2 | 4 | 55627.97197247496 |
| 3 | 6 | 55627.97197247496 |
| 4 | 7 | 55627.97197247496 |
| 5 | 8 | 55627.97197247496 |
| 6 | 9 | 55627.97197247496 |
| 7 | 2 | 50293.50333209634 |
+---------------+---------+-------------------+
table 2 表2
+---------+-------+
| desc_id | total |
+---------+-------+
| 1 | 18150 |
| 4 | 18150 |
| 6 | 18150 |
| 7 | 18150 |
| 8 | 18150 |
| 9 | 18150 |
+---------+-------+
i want table1.column1 + table2.total 我想要table1.column1 + table2.total
table1 (desc_id has a value of 2 in table2 desc_id has no value of 2 so basically the result should be column1 + 0.00 table1(desc_id在table2中的值为2 desc_id没有值2,因此基本上结果应为column1 + 0.00
SELECT t1.column1 + COALESCE(t2.column2, 0)
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
Seems like I miss understood. 好像我很想念。 If you have explicitly added not null for the column while creating then the empty and null values should be stored as 0.
如果在创建时已为该列明确添加了非null值,则空值和null值应存储为0。
如果您认为自己的值也可以为null,则可以在sql中使用ISNULL。
table1.column2 + ISNULL(table2.column2,0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.