如何获得此结果Insqlserver? [英] How Can I Get This Result Insqlserver?
本文介绍了如何获得此结果Insqlserver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两张桌子。 Table1包含 Cus_id(主键)
和 Cus_Name
,它包含一些这样的记录
Cus_id Cus_Name
---------------
101 xxxx
102 ABC
103 XXXX
和Table2包含 Regis_id,Cus_id(外键)和sale_amount
&它包含这样的记录
Regis_id Cus_id sale_amount
------------------------------ ---
1 101 23000
我需要像以下那样投出
Cus_id Sales_Amount
--- -----------------
101 23000
102
103
这样的查询是什么?任何人都可以帮助我..
解决方案
使用左加入
使用前学习:
http://www.dotnet-tricks.com/Tutorial/sqlserver/W1aI140312-Different-Types-of-SQL-Joins.html [ ^ ]
选择 a.cus_id,b。 Sales_Amount 来自 Table1 as a Left join table2 a.Cus_id = b.Cus_id
试试这个:
SELECT t1.Cus_id,t1.Cus_Name,SUM(t2。金额) AS 金额
FROM 表1 AS t1 LEFT JOIN 表2 AS t2 ON t1.Cus_id = t2.Cus_id
GROUP BY t1.Cus_id
试试这段代码。
选择 a.Cus_id,b.sale_amount 来自 table2 bright join table1 a on b.Cus_id = a.Cus_id
Hi,
I have two tables. Table1 contains Cus_id(primary key)
and Cus_Name
and it contains some record like this
Cus_id Cus_Name
---------------
101 xxxx
102 ABC
103 XXXX
And Table2 contains Regis_id,Cus_id(foreign Key),and sale_amount
& it contains records like this
Regis_id Cus_id sale_amount
------------------------------ ---
1 101 23000
I need out put like as follow as
Cus_id Sales_Amount
--------------------
101 23000
102
103
What is the query to get out put like this ? can anybody help me pls..
解决方案
use Left Join
Learn Before Use it :
http://www.dotnet-tricks.com/Tutorial/sqlserver/W1aI140312-Different-Types-of-SQL-Joins.html[^]
select a.cus_id,b. Sales_Amount from Table1 as a Left join table2 on a.Cus_id=b.Cus_id
Try this:
SELECT t1.Cus_id, t1.Cus_Name, SUM(t2.Amount) AS Amount FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON t1.Cus_id = t2.Cus_id GROUP BY t1.Cus_id
Try this code.
select a.Cus_id,b.sale_amount from table2 b right join table1 a on b.Cus_id=a.Cus_id
这篇关于如何获得此结果Insqlserver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文