回 帖 发 新 帖 刷新版面

主题:求一可以实现以下需求的SQL语法.

DSDG_MSG表结构如下:
MOBILE    VARCHAR2(20)                
ADDTIME    DATE                            
MSG    VARCHAR2(300)                        
IDNUM    NUMBER(9)        
RETFLAG    VARCHAR2(1)

现在要求在页面上显示出以上几个字段,加上一个统计字段(统计该表中mobile和msg字段内容相同的记录的个数).我用了以下可以实现.
select a.*,b.count from dsdg_msg a ,
         (select mobile,msg, count(*) as count from dsdg_msg group by mobile,msg ) b 
         where a.mobile=b.mobile and a.msg=b.msg  
         order by a.addtime desc        

现在又多了一个需求,即是mobile和msg字段内容相同的记录只在页面上显示一次.
哪位高手可以实现

回复列表 (共4个回复)

沙发

完全可以。要在查找的列前面加distinct就可以满足你的要求了。
select a.*,b.count from dsdg_msg a ,
         (select distinct mobile,msg, count(*) as count from dsdg_msg group by mobile,msg ) b 
         where a.mobile=b.mobile and a.msg=b.msg  
         order by a.addtime desc

板凳

不行哦.
(select distinct mobile,msg, count(*) as count from dsdg_msg group by mobile,msg )
这里是可以显示相同的纪录一次,但是两个表联合查询后
select a.*,b.count from dsdg_msg a ,
         (select distinct mobile,msg, count(*) as count from dsdg_msg group by mobile,msg ) b 
         where a.mobile=b.mobile and a.msg=b.msg  
         order by a.addtime desc
得出的结果却是相同的纪录有几次

3 楼

select a*,count=(select count(1) from dsdg_msg
where mobile=a.mobile and msg=a.msg group by mobile,msg) from dsdg_msg a 
by a.addtime desc试一下

4 楼

在加一个distinct呢?

 select distinct a.*,b.count from dsdg_msg a ,
         (select mobile,msg, count(*) as count from dsdg_msg group by mobile,msg ) b 
         where a.mobile=b.mobile and a.msg=b.msg  
         order by a.addtime desc    

新手说错了,不要骂偶

我来回复

您尚未登录,请登录后再回复。点此登录或注册