最近遇到一个需求需要查出3个表的数据,并且有两个表不能连表查询,他们有各自的筛选条件,写出的sql语句如下:
select 'c' || ec.base_dep_code || '_' || ec.no id,
ec.base_dep_code || '_' || ec.no username,
ec.name,
'c' usertype,
bd.input_code base_dep_name,
ec.no
from ERP_CUSTOMER ec
left join base_dep bd
on bd.code = ec.base_dep_code
where 1 = 1
and Upper(ec.name || ec.base_dep_code || ec.no ||
ec.input_code) like '%CP%'
union all
select ee.id ID,
ee.name as username,
ee.name as Name,
ee.Input_Code usertype,
bd.short_name base_dep_name,
ee.id as NO
from ERP_EMPLOYEE ee
left join base_dep bd
on bd.code = ee.base_dep_code
where 1 = 1
and Upper(ee.name || ee.input_code || ee.email) like "cp"
'%CP%'
这样写很不灵活,遇到筛选条件一变就无法继续使用了,