如何使用条件A.salary * B.rate
创建连接表A和B的查询Gen.sequence
我期待的结果。
Table A
---------------------
customerID salary
A1 100
A2 200
A3 300
Table B
---------------------
customerID rate
A1 2
A2 3
A3 4
答案 0 :(得分:2)
SELECT t1.customerID,
COALESCE(t1.salary * t2.rate, 'NA') AS salary
FROM TableA t1
LEFT JOIN TableB t2
ON t1.customerID = t2.customerID
我在计算有效工资时使用了COALESCE
,以防任何客户的工资金额或费率为NULL
。
在这里演示:
答案 1 :(得分:0)
尝试此查询:
select a.customerID, a.salary * b.Rate salary
from TableA a inner join TableB b
on a.customerID = b.customerID
order by a.customerID
答案 2 :(得分:0)
如果费率不存在,你可以使用IFNULL,至少它应该返回工资而不是零:
SELECT t1.customerID,
(t1.salary * IFNULL(t2.rate, 1)) AS salary
FROM TableA t1
LEFT JOIN TableB t2 ON t1.customerID = t2.customerID