问题描述
我有两个表table_a和table_b,结构如下
表 _ a
Emp_id号
部门编号
表 _ b
保持 _ 身份证号码
丢弃 _ 身份证号码
Dept_no编号
表 _ a
emp_id dept_no
101 10
102 10
103 20
108 10
107 20
表 _ b
保留id丢弃id dept_no
101 108 10
107 103 20
我的问题是
如何从table_A中获取相同Dept_no的Table_B的keep_id和discard_id列中存在的所有emp_id。
结果应该如下所示
表 _ a
Emp_id号
部门编号
表 _ b
保持 _ 身份证号码
丢弃 _ 身份证号码
Dept_no编号
表 _ a
emp_id dept_no
101 10
102 10
103 20
108 10
107 20
表 _ b
保留id丢弃id dept_no
101 108 10
107 103 20
我的问题是
如何从table_A中获取相同Dept_no的Table_B的keep_id和discard_id列中存在的所有emp_id。
create table table_a(Emp_id号, dept_no number); insert into test_a values(102,10); insert into test_a values(103,20); insert into test_a values(107,20); insert into test_a values(108,10); insert into test_a values(119,30); commit; create table test_b(keep_empid number, dicard_empid, dept_no); insert into test_b values(101,108,10); insert into test_b values(103,107,20); insert into test_b values(110,187,10); commit;复制
结果应该如下所示
emp_id,dept_id 101,10 108,10 103,20 107,20复制
专家解答
请在发布脚本之前先测试一下!那些 “创建表格” 不起作用...
你到底在找什么?
来自test_a的值,emp_ids等于部门的保留/丢弃值?
还是部门的保留/丢弃值之间的值?
无论是哪一个,你只需要加入:
你到底在找什么?
来自test_a的值,emp_ids等于部门的保留/丢弃值?
还是部门的保留/丢弃值之间的值?
无论是哪一个,你只需要加入:
create table test_a(emp_id number, dept_no number); insert into test_a values(102,10); insert into test_a values(103,20); insert into test_a values(107,20); insert into test_a values(108,10); insert into test_a values(119,30); commit; create table test_b(keep_empid number, dicard_empid int, dept_no int); insert into test_b values(101,108,10); insert into test_b values(103,107,20); insert into test_b values(110,187,10); commit; select a.* from test_a a join test_b b on b.dept_no = a.dept_no and (a.EMP_ID = b.keep_empid or a.emp_id = dicard_empid) order by 2, 1; EMP_ID DEPT_NO 108 10 103 20 107 20 select a.* from test_a a join test_b b on b.dept_no = a.dept_no and a.EMP_ID between b.keep_empid and dicard_empid order by 2, 1; EMP_ID DEPT_NO 102 10 108 10 103 20 107 20复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
Oracle RAC 一键安装翻车?手把手教你如何排错!
Lucifer三思而后行
603次阅读
2025-04-15 17:24:06
【纯干货】Oracle 19C RU 19.27 发布,如何快速升级和安装?
Lucifer三思而后行
588次阅读
2025-04-18 14:18:38
XTTS跨版本迁移升级方案(11g to 19c RAC for Linux)
zwtian
496次阅读
2025-04-08 09:12:48
Oracle数据库一键巡检并生成HTML结果,免费脚本速来下载!
陈举超
479次阅读
2025-04-20 10:07:02
【ORACLE】记录一些ORACLE的merge into语句的BUG
DarkAthena
464次阅读
2025-04-22 00:20:37
Oracle 19c RAC更换IP实战,运维必看!
szrsu
440次阅读
2025-04-08 23:57:08
【ORACLE】你以为的真的是你以为的么?--ORA-38104: Columns referenced in the ON Clause cannot be updated
DarkAthena
438次阅读
2025-04-22 00:13:51
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
429次阅读
2025-04-17 17:02:24
火焰图--分析复杂SQL执行计划的利器
听见风的声音
371次阅读
2025-04-17 09:30:30
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
361次阅读
2025-04-15 14:48:05