如何获得分支ID和分支ID输出 [英] How to get branch id and with out branch id ouput
问题描述
我有第一张桌子如下
Branchid BranchName
1 A
2 B
3 C
4 D
5 E
我有第二张表格如下
Empid Empname Branchid
1 Ram 1
2 Suresh 2
3 Vinay 1
4 Rahul -
5 Sam 3
i希望得到的输出结合上面两个表分支id。哪一个分支ID在那里,分支ID不在那里
如何编写查询
我尝试了什么:
我有第一张表如下
Branchid BranchName
1 A
2 B
3 C
4 D
5 E
我有第二张表如下
Empid Empname Branchid
1 Ram 1
2 Suresh 2
3 Vinay 1
4 Rahul -
5 Sam 3
i希望得到的输出结合上面两个表分支id。其中一个分支ID和分支ID不存在
,如何编写查询
< blockquote>使用适当的 SQL联接 [ ^ ]实现您的目标,例如
选择b.branchid,b.branchname,e.empid,e。 empname来自branchtable b right join emptable e on
b.branchid = e.branchid
将给你
branchid branchname empid empname
1 A 1 Ram
2 B 2 Suresh
1 A 3 Vinay
NULL NULL 4 Rahul
3 C 5 Sam
选择信息从你的第二张桌子加入你的第一张桌子。左外连接是合适的。
见
SQL连接的可视化表示 [ ^ ]
您可能还会发现ISNULL函数有用
--- -----------首先----------------
选择e.branchid作为branchid,b.branchname,e。 empid,e.empname from branch b
加入empp e on b.branchid = e.branchid
-------- for second -----------
选择b.branchid作为branchid,b.branchname,e.empid,e.empname,e .branchid as brancid from branch b
left join empp e on b.branchid = e.branchid其中empid为null
I have first table as follows
Branchid BranchName
1 A
2 B
3 C
4 D
5 E
I have Second table as follows
Empid Empname Branchid
1 Ram 1
2 Suresh 2
3 Vinay 1
4 Rahul -
5 Sam 3
i want to get output of combine the above two tables branch id. for which one branch id is there and branch id is not there
for that how to write the query
What I have tried:
I have first table as follows Branchid BranchName 1 A 2 B 3 C 4 D 5 E I have Second table as follows Empid Empname Branchid 1 Ram 1 2 Suresh 2 3 Vinay 1 4 Rahul - 5 Sam 3 i want to get output of combine the above two tables branch id. for which one branch id is there and branch id is not there for that how to write the query
Use the appropriate SQL Joins[^] to achieve your objective, e.g.select b.branchid, b.branchname, e.empid, e.empname from branchtable b right join emptable e on b.branchid = e.branchidwill give you
branchid branchname empid empname 1 A 1 Ram 2 B 2 Suresh 1 A 3 Vinay NULL NULL 4 Rahul 3 C 5 Sam
Select information from your second table joined to your first. A left outer join would be appropriate.
See
Visual Representation of SQL Joins[^]
You might also find the ISNULL function useful
--------------for first----------------
select e.branchid as branchid,b.branchname,e.empid,e.empname from branch b
join empp e on b.branchid=e.branchid
--------for second-----------
select b.branchid as branchid,b.branchname,e.empid,e.empname,e.branchid as brancid from branch b
left join empp e on b.branchid=e.branchid where empid is null
这篇关于如何获得分支ID和分支ID输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!