如何将2个SQL查询合二为一 [英] How to combine 2 sql queries in one

查看:58
本文介绍了如何将2个SQL查询合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用sql server 2012.任何人都可以指导我如何将以下两个命令合并为一个。



Hi All,

I am using sql server 2012. Can anybody please guide how I can combine the following 2 commands into one.

Select distinct PROJN from PROJ
 JOIN [USER] ON PROJ.PROJWD=[USER].USERT WHERE ([USER].USERID=@USER)
 ;

Select distinct PROJN from PROJ
WHERE PROJ.PROJWD IN (SELECT DEPTN FROM DEPT WHERE DEPTH=@USER or deptd=@USER)





我要求生成的数据表具有两个查询的不同组合结果。

编码我在asp.net中使用vb。



提前谢谢..



I require the resultant datatable to have distinct combine result of both queries.
For coding I am using vb in asp.net.

Thanks in advance..

推荐答案

您也可以尝试这个

You can try this also
Select distinct PROJN from PROJ
 JOIN [USER] ON PROJ.PROJWD=[USER].USERT WHERE ([USER].USERID=@USER)
or PROJ.PROJWD IN (SELECT DEPTN FROM DEPT WHERE DEPTH=@USER or deptd=@USER)


You can use either union or union all to combine the two queries.


Select distinct PROJN from PROJ
 JOIN [USER] ON PROJ.PROJWD=[USER].USERT WHERE ([USER].USERID=@USER)

union/Union all

Select distinct PROJN from PROJ
WHERE PROJ.PROJWD IN (SELECT DEPTN FROM DEPT WHERE DEPTH=@USER or deptd=@USER)


使用UNION解决了这个问题由PIEBALDconsult点击。



谢谢
Use of UNION has solved the matter as suggested by PIEBALDconsult.

Thanks


这篇关于如何将2个SQL查询合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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