mysql_oracle_随机查询几条记录

数据库的随机查询SQL 
 

1. Oracle,随机查询20条

select * from

(
 select  *  from 表名
 order by dbms_random.value

)
 where rownum <= 20;

2.MS SQL Server,随机查询20条

select top 20  * from  表名order by newid()

3.My SQL:,随机查询20条

select  *  from  表名 order by rand() limit 20

随机查询指定人员的一条未读消息
 
帮助消息表 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  --取一条

原文地址:https://www.cnblogs.com/cphmvp/p/4126559.html