旋转的钢笔 2018-07-03 09:29 采纳率: 54.5%
浏览 530
已采纳

oracle查询计划信息,如何分隔关联查询?

现要查询计划表信息,和组织权限相关联,目前sql
T_TRAIN_PLAN 表结构
图片说明

图片说明
t_employee_organization_ref表结构
图片说明
图片说明

 select *
  from (select distinct t.plan_name,
                        t.plan_code,
                        t.begin_date,
                        t.end_date,
                        t.id,
                        t.status,
                        t.verify,
                        t.isDel,
                        ROWNUM RN
          from (select t.plan_name,
                       t.plan_code,
                       t.begin_date,
                       t.end_date,
                       t.id,
                       t.status,
                       t.verify,
                       row_number() over(order by create_time desc) as seqnum,
                       (select count(1)
                          from t_train_session ts
                         where ts.plan_id = t.id
                           and ts.train_begin_time < sysdate) isDel
                  FROM (SELECT *
                          FROM (SELECT t.store_id,
                                       t.plan_name,
                                       t.plan_code,
                                       t.begin_date,
                                       t.end_date,
                                       t.id,
                                       t.status,
                                       t.verify,
                                       create_time,
                                       t.remark  mark
                                  FROM T_TRAIN_PLAN t
                                 where t.status in ('0', '2') and t.remark is not null ) t,
                               t_employee_organization_ref a 
                         where t.store_id = a.unit_id  
                           and a.employee_code = 'admin') t) t
         where seqnum <=10
         order by t.begin_date desc)
 where rn > 0;


 抽取主体也就是

  SELECT *
                          FROM (SELECT t.store_id,
                                       t.plan_name,
                                       t.plan_code,
                                       t.begin_date,
                                       t.end_date,
                                       t.id,
                                       t.status,
                                       t.verify,
                                       create_time,
                                       t.remark  mark
                                  FROM T_TRAIN_PLAN t
                                 where t.status in ('0', '2') and t.remark is not null ) t,
                               t_employee_organization_ref a 
                         where t.store_id = a.unit_id  
                           and a.employee_code = 'admin'    

目前的业务是,当创建计划时,Store_id可以选择多个, 保存的时候,如果store_id是多个,则保存在了remark字段中,如果只是一个保存在store_id字段中, 那么问题
来了,如果store_id是多个,已逗号分隔的形式保存在了remark字段中,我怎么和t_employee_organization_ref 组织权限表关联啊? 并且查找出计划信息。。。

  • 写回答

3条回答

  • cfm999 2018-07-03 14:36
    关注

    SELECT t.*
    FROM t_train_plan t
    INNER JOIN t_employee_organization_ref a
    ON (','|| t.remark ||',' LIKE '%,'|| a.unit_id ||',%'
    OR t.store_id = a.unit_id)
    AND t.status IN ('0', '2')
    AND a.employee_code = 'admin';

    --REMARK多个store_id,一条t_train_plan记录在结果中可能有多条
    --语句未实际测试
    --另外,贴中语句嵌套层次太多,建议尽量避免子查询!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘