举例多表链接

1.查询有费用凭单的男病人的姓名,费用发生日期,开凭单的医生姓名,凭单所属的科室名称

//xm值重复,重新命名别名
select a.xm brxm,b.fyfsrq,c.xm ysxm,d.ksmc
from hr.per_natl a,
	hr.patient_script_genl b
	hr.doctor_ c
	hr.detp d
//关联条件
where a.grbm=b.grbm
	and b.ysbm = c.ysbm
	and b.ksbm = c.ksbm
	and a.xb='1'

2.查询有费用凭单的病人姓名,用过的药品名称,药品单价

//distinct去重
select distinct a.xm brxm,d.ylxmmc ypmc,d.dj ypdj
 from hr.per_natl a,
 	 hr.petaient_script_genl b,
 	 hr.patient_script_detl c,
 	 hr.medi_item d
 //关联条件
 where a.grbm = b.grbm
 	and b.fyid=c.fyid
 	and c.ylxmbm = d.ylxmbm
 	and d.ylxmlb='1'

3.查询有费用凭单的病人的姓名,用过的药品的名称、单价,使用过的次数,使用的总数量

//select语句超过五个字段放下一行
//group by时不需要distinct(去重)
select a.xm brxm,d.ylxmmc ypmc,d.dj ypdj,
  count(c.ylxmbm) sycs,sum(c.sl) zsl
 from hr.per_natl a,
 	 hr.petaient_script_genl b,
 	 hr.patient_script_detl c,
 	 hr.medi_item d
 //关联条件
 where a.grbm = b.grbm
 	and b.fyid=c.fyid
 	and c.ylxmbm = d.ylxmbm
 	and d.ylxmlb='1'
//分组不需要别名
group by d.ylxmbm,a.xm,d.ylxmmc,d.dj;

4.查询开过凭单的医生的姓名,所属科室名称

select a.xm,b,ksmc
 from hr.doctor a,hr.dept b
where a.ksbm = b.ksbm
//判定存在费用记录 select 常量,只是用作组成结构
and exists(select '1' hr.patient_script_genl g
		where g.ysbm = a.ysbm);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值