本文介绍了如何获得分支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不在那里 如何编写查询 我尝试了什么: 我有第一张表如下 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不存在 ,如何编写查询 解决方案 使用适当的 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 EI 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 therefor that how to write the queryWhat I have tried: I have first table as follows Branchid BranchName 1 A 2 B 3 C 4 D 5 EI 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 therefor 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 onb.branchid = e.branchidwill give you branchidbranchnameempidempname1 A 1 Ram2 B 2 Suresh1 A 3 VinayNULL NULL 4 Rahul3 C 5 SamSelect information from your second table joined to your first. A left outer join would be appropriate.SeeVisual 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 bjoin 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 bleft join empp e on b.branchid=e.branchid where empid is null 这篇关于如何获得分支ID和分支ID输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 06:58