首页 > 其他 > 详细

聚合函数下查询多个列

时间:2018-01-26 10:07:46      阅读:224      评论:0      收藏:0      [点我收藏+]

查询聚合函数符合某个条件的记录,只能查询聚合的列,不能查询其他列,例如查询手机号出现一次以上的记录,只能下面这样

select Phone,count(phone)
from PeisPatient group by Phone having count(phone)>1

 

加上其他列就不行

select PatientCode,Phone,count(phone),IDCardNo,PatientName,ID_Patient
from PeisPatient group by Phone,PatientCode,IDCardNo,PatientName,ID_Patient having count(phone)>1

但可以用个性能不太好的语句实现:

select PatientCode,IDCardNo,PatientName,ID_Patient,phone from PeisPatient where phone in
(
select Phone
from PeisPatient group by Phone having count(phone)>1 ) order by phone

 

聚合函数下查询多个列

原文:https://www.cnblogs.com/yanan7890/p/8358169.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!