用于表的SQL查询比较 [英] sql query for tables compare

查看:102
本文介绍了用于表的SQL查询比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我有两张桌子如下,



表1

Hi friends,
I am having 2 tables as follows,

Table 1

ID      Name    PayAmount
1       A        1000
2       B        2000
3       C        1000





表2



Table 2

ID          PaidAmount
 1           600
 2           2000
 4           1000





这里我需要输出如下



Here i need output as follows

ID         Name   BalanceAmt
 1           A     400
 3           c     1000

推荐答案

假设Table2中的id只有一行在你的例子中:

Supposing there is only one row for an id in Table2 as in your example:
select
 t1.ID, t1.Name, t1.PayAmount-coalesce(t2.PaidAmount,0) as BalanceAmt
 from Table1 t1
 left join Table2 t2 on t1.ID = t2.ID
 where
 t1.PayAmount-coalesce(t2.PaidAmount,0) > 0
 order by t1.ID


这是查询



Here is query

SELECT T1.Id AS Id,
       T1.Name AS Name,
       (T1.PayAmount - T2.PaidAmount) AS BalanceAmt
FROM Table1 AS T1 LEFT OUTER JOIN Table2 AS T2 ON T1.Id=T2.Id
WHERE t1.PayAmount-coalesce(t2.PaidAmount,0) > 0





希望这有帮助,如果是,那么接受和如果没有投票给答案,那么请回复你的查询\

--Rahul Dhoble



Hope this helps if yes then accept and vote the answer if not then revert back with your queries\
--Rahul Dhoble


这篇关于用于表的SQL查询比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆