首页 新闻 搜索 专区 学院

sql server 两表表合并

0
悬赏园豆:30 [已解决问题] 解决于 2016-05-19 09:30

我是查询出来的两张数据表,现在需要合并为一张表方便使用,请看sql语句

SELECT  A.User_Name  ,A.PhoneNo  ,A.OICQ  ,C.RoleCode  ,D.Value AS RoleName  ,G.CompanyName  ,G.Address AS CompanyAddress  ,G.Telephone AS CompanyTel,F.PropertyInstance_Value FROM  Base_UserInfo A LEFT JOIN  Base_UserRole B ON  A.User_ID = B.User_ID LEFT JOIN  Base_Roles C ON  B.Roles_ID = C.Roles_ID LEFT JOIN  TMSTCommonData D  ON C.RoleCode = D.Code AND D.CodeKey = 'ChatUserRole',  TMSTCompany G,Base_AppendPropertyInstance F WHERE A.CompanyCode = G.CompanyCode
  AND (A.User_ID=F.PropertyInstance_Key  AND F.Property_Control_ID='QrWeChatPath')  AND A.User_Account = '18381091656'

 

 SELECT F.PropertyInstance_Value as Usercode from Base_UserInfo A,Base_AppendPropertyInstance F where A.User_Account='18381091656' and A.User_ID=F.PropertyInstance_Key
  AND F.Property_Control_ID='PromotionCode'

有人私下帮我解决下更好,我可以给你发图详细看

jinggege的主页 jinggege | 初学一级 | 园豆:5
提问于:2016-05-11 11:55
< >
分享
最佳答案
0

---说明:Sql 之union 的方法,有关资料,可以百度了解。以下是写好的Sql语句,请自己试下。

 

SELECT A.User_Name ,
A.PhoneNo ,
A.OICQ ,
C.RoleCode ,
D.Value AS RoleName ,
G.CompanyName ,
G.Address AS CompanyAddress ,
G.Telephone AS CompanyTel,
F.PropertyInstance_Value as Usercode
FROM Base_UserInfo A LEFT JOIN Base_UserRole B ON A.User_ID = B.User_ID LEFT JOIN Base_Roles C ON B.Roles_ID = C.Roles_ID LEFT JOIN TMSTCommonData D ON C.RoleCode = D.Code AND D.CodeKey = 'ChatUserRole', TMSTCompany G,Base_AppendPropertyInstance F WHERE A.CompanyCode = G.CompanyCode
AND (A.User_ID=F.PropertyInstance_Key AND F.Property_Control_ID='QrWeChatPath') AND A.User_Account = '18381091656'

union

SELECT
'' as User_Name ,
'' as PhoneNo ,
'' as OICQ,
'' as RoleCode,
'' as RoleName,
'' as CompanyName,
'' as CompanyAddress,
'' as CompanyTel,
F.PropertyInstance_Value as Usercode
from Base_UserInfo A,Base_AppendPropertyInstance F where A.User_Account='18381091656' and A.User_ID=F.PropertyInstance_Key
AND F.Property_Control_ID='PromotionCode'

收获园豆:30
chenaran | 菜鸟二级 |园豆:283 | 2016-05-11 12:33

大哥,把你QQ给我,我发图给你看看

jinggege | 园豆:5 (初学一级) | 2016-05-11 12:58

@jinggege: N年前就不用QQ了,可以把图片放到这里来。

chenaran | 园豆:283 (菜鸟二级) | 2016-05-11 13:49

@chenaran: 还是这个排版看着舒服~

小刺猬001 | 园豆:658 (小虾三级) | 2016-05-11 14:03
其他回答(1)
0

可以使用虚拟表,然后创建个视图,这样挺方便的。

http://www.jianshu.com/p/1f6f9d5f8940

seayxu | 园豆:5468 (大侠五级) | 2016-05-11 12:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册