随机查询指定人员的一条未读消息
 
帮助消息表 S_MSG_HINT
帮助消息ID SMH_ID NUMBER(20) PK
帮助消息内容 SMH_TEXT VARCHAR2(200)
 
人员帮助消息表 S_HINTPEOPLE
人员ID SHP_UID VARCHAR2(20) PK
当前帮助消息ID SMH_ID NUMBER(20)  PK FK
 
如果为已读消息会在 人员帮助消息表里面生成记录
 
select
    *
from
    (select
        smh.*,
        nvl2( shp.smh_id,1,0) as status --0:未读 1:已读
    from
        s_msg_hint smh
                left join s_hintpeople shp 
                    on smh.smh_id = shp.smh_id 
                    and shp.shp_uid = 'p_chencc'
    order by
        dbms_random.value  --随机数值排序
    )
where
    status = 0 and --未读消息
    rownum <= 1  --取一条
 
查询随机的记录的各个数据库的语法
 
1. Oracle,随机查询10条
select * from
(
 select  *  from table1
 order by dbms_random.value
)
 where rownum <= 20;
 
2.MS SQL Server,随机查询10条
select top 10  * from  table1 order by newid()
 
3.My SQL:,随机查询10条
select  *  from  table1 order by rand() limit 20 
 
  
	posted on 2013-11-02 11:35 
sanmao 阅读(885) 
评论(2)  编辑  收藏