简体   繁体   English

从表1连接表2中选择id,table1.column1 + table2.column2作为总数。 如果table2.column2没有值怎么办?

[英]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.

相关问题 SQL INNER JOIN ON table1.column1 = table2.column2或ON ..? - SQL INNER JOIN ON table1.column1 = table2.column2 OR ON ..? 如何将table1.column1的值设置为table2.column2的平均值 - How to set value of table1.column1 to an average of table2.column2 提取table1.column2的总和,其中table1.column1包含来自table2.column2的值 - Extract a sum of table1.column2 where table1.column1 contains value from table2.column2 选择最后一个不同的记录,其中 table1.column1 = table2.column2 - Select last distinct records where table1.column1 = table2.column2 mysql更新表设置column3,其中table1.column1像concat('%',table2.column2,'%') - mysql update table set column3 where table1.column1 like concat ('%',table2.column2,'%') SELECT *(table1),列名(table2)FROM table1 JOIN table2 - SELECT *(table1), column name (table2) FROM table1 JOIN table2 [table1.column1=table2.column1] 和 [table1.column1=1 AND table2.column1=1] 之间的区别 - Difference between [table1.column1=table2.column1] and [table1.column1=1 AND table2.column1=1] MYSQL - Select 列等于 table1 列如果 table1 列不是 null - MYSQL - Select column from table2 equal to table1 column if table1 column not null 如何在MySQL选择左连接中用table2列值替换table1列值 - How to replace table1 column value with table2 column value in MySQL select left join 在table1的指定列之后如何将table2的列连接到table1? - How to join column from table2 to table1 after a specified column of table1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM