码迷,mamicode.com
首页 > 其他好文 > 详细

聚合函数下查询多个列

时间:2018-01-26 10:56:43      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:select   code   sel   phone   count   car   ati   idc   div   

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

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

 

聚合函数下查询多个列

标签:select   code   sel   phone   count   car   ati   idc   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!