自学数据库之一个自己写的SQL语句

本文介绍了一个具体的SQL查询案例,展示了如何从多个表中联接数据并进行条件筛选,特别是使用case语句处理性别和年龄单位的转换,以及通过子查询获取科室名称和申请医生的名字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,自己写的查询语句:

select   b.yq,b.ybh,
a.ksdh,
a.sjys,
a.brdh as 住院号,
b.num_result as 检验结果,
b.jyrq as 检验日期,
a.brxm as 病人姓名,
(case a.brxb when '1' then '男' when '2' then '女' when 'F' then '女' when 'M' then '男' else '其他' end ) as 病人性别,
a.nl as 年龄,
(case a.nldw when '1' then '岁' when '2' then '月' when '3' then '天' when '4' then '时' else '其他' end ) as 年龄单位,
a.zd as 临床诊断,
(select zdmc from com_dict c where a.ksdh=c.zddh and zdlb='DP') as '科室/病区',
(select zdmc from com_dict c where a.sjys=c.zddh) as 申请医生
from lis_result b ,lis_pat a
where b.yq = a.yq and b.ybh=a.ybh and b.jyrq=a.jyrq
 and b.jyrq>='2015-04-01' and b.jyrq<='2015-06-14' and  b.xmdh='PLT' and b.num_result <75 order by b.jyrq

2,分析:

case。。。when.....then......when......then......else.........end

(select zdmc from com_dict c where a.ksdh=c.zddh and zdlb='DP') as '科室/病区', z注:加zdlb='DP'是为了解决返回多个值的问题
(select zdmc from com_dict c where a.sjys=c.zddh) as 申请医生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值