SQL高手请进

hisquong 2006-04-06 03:48:45
表名:Wj
表中数据如下:
wjId docId type
1 1 公文
2 1 通知
3 2 公文
4 2 通知
5 3 公文
6 3 报告

现在我想查找docId,要求满足的条件是:type为“公文”或“通知”,并且docId相同。
(例如:这里docId为“1”、“2”的记录满足条件;而docId为“3”的记录不满足)
请教SQL语句,可以查询出上面满足条件的docId。
注:如果是用select docId from Wj where type='公文' and docId in(select docId from Wj where type='通知'),这样的SQL就免了,我想求高效点的^_^
...全文
179 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
拓狼 2006-04-06
  • 打赏
  • 举报
回复
select a.wjid,b.wjid from wj a inner join wj b on a.docid=b.docid
where a.type='公文' and b.type='通知'
xeqtr1982 2006-04-06
  • 打赏
  • 举报
回复
exists比in的效率高一些吧?
lsqkeke 2006-04-06
  • 打赏
  • 举报
回复
select docId from Wj t
where type='公文' and exists(select 1 from Wj where docId=t.docId and type='通知')
eddy8863 2006-04-06
  • 打赏
  • 举报
回复
select distinct docId from Wj a
where exists(select 1 from wj where docId=a.docId and type='公文')
and exists(select 1 from wj where docId=a.docId and type='通知')

22,209

社区成员

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

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