MS SQL连接 [英] MS SQL Joins

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

问题描述

亲爱的所有人,
我有一个问题要从下面的三个表中获取数据..

表A
客户名称Mth tbbTotal
XYZ 04 0.30

表B
客户名称Mth pendAmt
XYZ 05 0.30
XYZ 06 0.30
XYZ 07 0.30

表C
custName Mth collAmt
XYZ 08 0.30

我需要以下要塞的​​结果

custName Mth tbbAmt pendAmt collAmt
XYZ 04 0.30 0.00 0.00
XYZ 05 0.00 0.30 0.00
XYZ 06 0.00 0.30 0.00
XYZ 07 0.00 0.30 0.00
XYZ 08 0.00 0.00 0.30


要求所有专家请帮助我获得上述结果.
在此先感谢.

Dear All,
I have one issue to get the data from three table as below..

Table A
CustName Mth tbbTotal
XYZ 04 0.30

Table B
CustName Mth pendAmt
XYZ 05 0.30
XYZ 06 0.30
XYZ 07 0.30

Table C
custName Mth collAmt
XYZ 08 0.30

I have required result in following fortmat

custName Mth tbbAmt pendAmt collAmt
XYZ 04 0.30 0.00 0.00
XYZ 05 0.00 0.30 0.00
XYZ 06 0.00 0.30 0.00
XYZ 07 0.00 0.30 0.00
XYZ 08 0.00 0.00 0.30


Request to all expert please help me to get the above result.
Thanks in Advance.

推荐答案



您可以在My SQL中对表使用并集或并集全部

请参见下面的语法

Hi

You can use either union or union all for your tables in My SQL

See the Syntax below

(SELECT * FROM t1)
UNION [ALL]
(SELECT * FROM t2 )




如果要从所有表中获得唯一记录,则只能使用联合",但如果要使用所有记录中所有表中都具有唯一记录重复的记录,则可以使用联合全部".当您使用"Union"或"Union All"时,所有表中的列数应相同,并且它们的类型也应匹配.

希望这会对您有所帮助.




If you want unique records from all the tables then you can use "Union" only, but if you want to use all the records with duplication of unique records from all the tables then you can use "Union All". When you are using "Union" or "Union All", the count of columns in all the tables should be same with matching their type also.

Hope this will help you.


尝试加入CustNameMth上的三个表:

Try joining the three table on CustName and Mth:

select a.CustName, a.Mth, a.tbbTotal, b.pendAmt, c.collAmt
from
Table_A a join Table_B b on (a.CustName = b.CustName and a.Mth = b.Mth)
join Table_C on (a.CustName = c.CustName and a.Mth = c.Mth)


感谢您的快速回复,
可以这么说,您给定的sql不会显示正确的结果.结果集缺少该值,原因是内部联接,因为表A,表B和表C中的月份不匹配.仍然有待解决..
Thanks for quick reply,
Sory to say, that your given sql not show propoer result. result set missing the value, due the inner join because month not matched in Table A , Table B and Table C. Still issue are pending..


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

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