SQL Left Join问题 解决立马结帖
EMR_ProcessUReadMessage表数据如下:一共6条
EMR_Code CreateByPlatform IsRead
201707130001 Doctor 1
201707130001 Desgin 0
201707130001 Medicine 1
201707130001 Doctor 1
201707130001 Desgin 0
201707130001 Medicine 1
doctor_essays 表 两条记录:数据如下
EMR_Code doctorRemarkDate doctorRemark
201707130001 2017.07.14 14:19:00 添加一条未读信息
201707130001 2017.07.14 14:08:42 测试未读
simple_EMR表数据只有一条:
EMR_Code patient_Name patient_Age patient_Sex patient_Tel
201707130001 测试医学服务中心New 2017-08-16 1 15221105698
SQL语句如下:
查询出来的结果如下:
EMR_Code isRead RecordType CreateByPlatform message createDate
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
最终我想得到结果如下:请问各位高手如何实现??
EMR_Code isRead RecordType CreateByPlatform message createDate
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
EMR_Code CreateByPlatform IsRead
201707130001 Doctor 1
201707130001 Desgin 0
201707130001 Medicine 1
201707130001 Doctor 1
201707130001 Desgin 0
201707130001 Medicine 1
doctor_essays 表 两条记录:数据如下
EMR_Code doctorRemarkDate doctorRemark
201707130001 2017.07.14 14:19:00 添加一条未读信息
201707130001 2017.07.14 14:08:42 测试未读
simple_EMR表数据只有一条:
EMR_Code patient_Name patient_Age patient_Sex patient_Tel
201707130001 测试医学服务中心New 2017-08-16 1 15221105698
SQL语句如下:
SELECT dess.EMR_Code , H.isRead, 'Remark' as RecordType,H.CreateByPlatform,
dess.doctorRemark AS 'message' ,
dess.createDate
FROM dbo.doctor_essays dess
LEFT join dbo.simple_EMR E on dess.EMR_Code=E.EMR_Code
LEFT join EMR_ProcessUReadMessage H on H.EMR_Code=dess.EMR_Code
WHERE dess.EMR_Code = '201707130001' and H.CreateByPlatform='Doctor'
AND dess.flag = 1查询出来的结果如下:
EMR_Code isRead RecordType CreateByPlatform message createDate
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
最终我想得到结果如下:请问各位高手如何实现??
EMR_Code isRead RecordType CreateByPlatform message createDate
201707130001 1 Remark Doctor 添加一条未读信息 2017-07-14 14:19:04.167
201707130001 1 Remark Doctor 测试未读 2017-07-14 14:08:45.170
...全文
相关推荐
SQL中的left join right join
数据库常见的join方式有三种:inner join, left outter join, right outter join(还有一种full join,因不常用,本文不讨论)。这三种连接方式都是将两个以上的表通过on条件语句,拼成一个大表。
SQL语句优化之JOIN和LEFT JOIN 和 RIGHT JOIN语句的优化
在数据库的应用中,我们经常需要对数据库进行多表查询,然而当数据量非常大时多表查询会对执行效率产生非常大的影响,因此我们在使用JOIN和LEFT JOIN 和 RIGHT JOIN语句时要特别注意
SQL之left join、right join、inner join的区别浅析
主要介绍了SQL之left join、right join、inner join的区别浅析,本文讲解了它们的区别并给出了实例,需要的朋友可以参考下
Sql语句用left join解决多表关联问题(关联套关联,例子和源码)
Sql语句用left join解决多表关联问题(关联套关联,例子和源码)一看就明白多表关联如何写SQl语句
SQL中的left outer join,inner join,right outer join用法详解
SQL中的left outer join,inner join,right outer join用法详解


