oracle中单字段存在1个或2个关联id时的查询sql

//上午值班人数为1人或2人时,查出值班信息和个人信息
select 
b.contact as mContact,b.email as mEmail,
c.contact as m2Contact,c.email as m2Email,
t.* from(
select
substr(a.morning_people_ids,0,instr(a.morning_people_ids,',',1,1)-1) as amId,
substr(a.morning_people_ids,instr(a.morning_people_ids,',',1,1)+1) as bmId,
a.*
from tb_duty a 
) t,
tb_duty_person_info b,
tb_duty_person_info c
where 1=1 and
nvl2(t.amId,t.amId,t.bmId)=b.person_id 
and t.bmId=c.person_id 
and t.dscd='340100' and t.unit_id=4 
and t.day0='2015-08-06'



//整个8月的
select 
m1.person_name as mName,
m1.contact as mContact,m1.email as mEmail,
m2.person_name as m2Name,
m2.contact as m2Contact,m2.email as m2Email,
a1.person_name as aName,
a1.contact as aContact,a1.email as aEmail,
a2.person_name as a2Name,
a2.contact as a2Contact,a2.email as a2Email,
e1.person_name as eName,
e1.contact as eContact,e1.email as eEmail,
e2.person_name as e2Name,
e2.contact as e2Contact,e2.email as e2Email,
l1.person_name as lName,
l1.contact as lContact,l1.email as lEmail,
l2.person_name as l2Name,
l2.contact as l2Contact,l2.email as l2Email,
t.week_day,
t.flag,
t.day0
 from(
select
substr(a.morning_people_ids,0,instr(a.morning_people_ids,',',1,1)-1) as mId,
substr(a.morning_people_ids,instr(a.morning_people_ids,',',1,1)+1) as m2Id,

substr(a.afternoon_people_ids,0,instr(a.afternoon_people_ids,',',1,1)-1) as aId,
substr(a.afternoon_people_ids,instr(a.afternoon_people_ids,',',1,1)+1) as a2Id,

substr(a.evening_people_ids,0,instr(a.evening_people_ids,',',1,1)-1) as eId,
substr(a.evening_people_ids,instr(a.evening_people_ids,',',1,1)+1) as e2Id,

substr(a.leader_ids,0,instr(a.leader_ids,',',1,1)-1) as lId,
substr(a.leader_ids,instr(a.leader_ids,',',1,1)+1) as l2Id,

a.*
from tb_duty a 
) t,
tb_duty_person_info m1,
tb_duty_person_info m2,
tb_duty_person_info a1,
tb_duty_person_info a2,
tb_duty_person_info e1,
tb_duty_person_info e2,
tb_duty_person_info l1,
tb_duty_person_info l2
where 1=1 
and
nvl2(t.mId,t.mId,t.m2Id)=m1.person_id 
and t.m2Id=m2.person_id 
and
nvl2(t.aId,t.aId,t.a2Id)=a1.person_id 
and t.a2Id=a2.person_id 
and
nvl2(t.eId,t.eId,t.e2Id)=e1.person_id 
and t.e2Id=e2.person_id 
and
nvl2(t.lId,t.lId,t.l2Id)=l1.person_id 
and t.l2Id=l2.person_id 
and t.dscd='340100' and t.unit_id=4 
and to_char(to_date(t.day0,'yyyy-mm-dd'),'yyyy-mm')='2015-08'

附:
//查询tb_duty_person_info第11-20条记录(根据person_id排序):
   //0.031秒
   select a.* from (
   select rownum rn ,b.* from tb_duty_person_info b  where rownum<21 order by b.person_id
   ) a 
   where a.rn>10;
   //0.047秒
      select a.* from (
   select rownum rn ,b.* from tb_duty_person_info b order by b.person_id
   ) a 
   where a.rn>10 and rn<21 ;

好的,以下是一个数据库系统 SQL Server 实验中单查询的实验结果分析。 单表查询实验通常旨在通过 SQL 语句查询单个表格中的数据,并对查询结果进行分析和解释。在 SQL Server 中,可以使用 SELECT 语句来进行单表查询,例如: ``` SELECT * FROM tableName; ``` 这个语句将检索表格中的所有数据,并将其返回到查询结果中。 在单表查询实验中,你可以执行多种类型的查询,例如: 1. 简单查询:使用基本的 SELECT 语句查询表格中的数据。例如: ``` SELECT firstName, lastName FROM employees; ``` 这个查询将返回表格中所有员工的名字和姓氏。 2. 条件查询:使用 WHERE 子句过滤表格中的数据。例如: ``` SELECT * FROM employees WHERE department = 'Sales'; ``` 这个查询将返回所有在销售部门工作的员工的信息。 3. 聚合查询:使用聚合函数(如 COUNT、AVG、MAX、MIN 和 SUM)对表格中的数据进行计算。例如: ``` SELECT COUNT(*) FROM employees; ``` 这个查询将返回表格中员工的总数。 4. 排序查询:使用 ORDER BY 子句对查询结果进行排序。例如: ``` SELECT * FROM employees ORDER BY hireDate DESC; ``` 这个查询将返回按照入职日期降序排列的所有员工信息。 在分析单表查询实验结果,你需要考虑以下因素: 1. 查询语句的正确性:确保查询语句正确无误,并且可以返回你所期望的结果。 2. 查询结果的准确性:分析查询结果,确保其包含完整、准确的数据。 3. 查询效率:评估查询语句的执行效率,尤其是对大型数据集进行查询。 4. 数据库性能:分析查询对数据库性能的影响,例如查询语句是否会锁定表格或影响其他查询的执行。 通过对这些因素进行分析,你可以更好地理解单表查询实验的结果,并对数据库系统的性能和优化提出建议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值