现要查询计划表信息,和组织权限相关联,目前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 组织权限表关联啊? 并且查找出计划信息。。。
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记录在结果中可能有多条
--语句未实际测试
--另外,贴中语句嵌套层次太多,建议尽量避免子查询!
没用过Oracle,用SQLserver多,一个公司总会有相似,试试用逗号或者外链接
建议你换个 角度来分析这个问题。怎么能用这么多的嵌套呢。建议你优化下sql语句吧。 不妨你你的这个表和需求发出来,让大家帮你出sql 也可以。现在的这个嵌套太多,在怎么优化 效率也难以提上去。