如何同时查询2个库的表进行对比?

恶魔的幻影 2014-02-20 12:51:41
如何同时查询2个库的表进行对比?
库1:qpqxbs001 表:Card
库2:hyzfzgl 表:NewMemberVisit

目的: 当下面代码查询qpqxbs001库的Card表的数据 查询出来的 card_no列和hyzfzgl库的NewMemberVisit表的card_no列进行对比,如果NewMemberVisit表里面有这个数据就不显示了

说白了就是2个表的数据对比 如果NewMemberVisit有了这条数据就不显示出来了!

现在是单库查询代码

SELECT a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji,a.gongsiNo ,a.gongsiMc ,MAX(b.work_no) work_no ,b.xche_ssje ,min(c.wxxm_mc) wxxm_mc FROM qpqxbs001.dbo.Card a LEFT JOIN work_pz_sj b ON a.card_no = b.card_no LEFT JOIN work_mx_sj c ON b.work_no = c.work_no WHERE a.gongsiNo='02' and a.Card_jlrq >='2014-02-01' and a.Card_jlrq <='2014-02-20' GROUP BY a.card_no ,a.Card_jlrq , a.card_kehu_mc ,a.card_kehu_shouji ,a.gongsiNo ,a.gongsiMc ,b.xche_ssje ORDER BY card_jlrq DESC
...全文
56 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LongRui888 2014-02-20
  • 打赏
  • 举报
回复
SELECT a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji, a.gongsiNo ,a.gongsiMc ,MAX(b.work_no) work_no , b.xche_ssje ,min(c.wxxm_mc) wxxm_mc FROM qpqxbs001.dbo.Card a LEFT JOIN work_pz_sj b ON a.card_no = b.card_no LEFT JOIN work_mx_sj c ON b.work_no = c.work_no WHERE a.gongsiNo='02' and a.Card_jlrq >='2014-02-01' and a.Card_jlrq <='2014-02-20' and not exists(select 1 from hyzfzgl.dbo.NewMemberVisit n where a.card_no = n.card_no) GROUP BY a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji, a.gongsiNo,a.gongsiMc ,b.xche_ssje ORDER BY card_jlrq DESC
LongRui888 2014-02-20
  • 打赏
  • 举报
回复
修改一下: SELECT a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji, a.gongsiNo ,a.gongsiMc ,MAX(b.work_no) work_no , b.xche_ssje ,min(c.wxxm_mc) wxxm_mc FROM qpqxbs001.dbo.Card a LEFT JOIN work_pz_sj b ON a.card_no = b.card_no LEFT JOIN work_mx_sj c ON b.work_no = c.work_no WHERE a.gongsiNo='02' and a.Card_jlrq >='2014-02-01' and a.Card_jlrq <='2014-02-20' and exists(select 1 from hyzfzgl.dbo.NewMemberVisit n where a.card_no = n.card_no) GROUP BY a.card_no ,a.Card_jlrq ,a.card_kehu_mc ,a.card_kehu_shouji, a.gongsiNo,a.gongsiMc ,b.xche_ssje ORDER BY card_jlrq DESC

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧