思路: 先将取出的值随机排序,然后在随机排序的每次取第一条的结果 举例如下: select * from(select t.code fromTBIZOPS_PROVINCE t ORDER BY DBMS_RANDOM.RANDOM())where rownum < 2;select * from (select t.code from TBIZOPS_CITY t wheret.province_id = '13' ORDER BY DBMS_RANDOM.RANDOM()) where
SQL Server SELECT TOP N * FROM 表名 ORDER BY NEWID() My SQL SELECT * FROM 表名 ORDER BY RAND() LIMIT N Oracle SELECT * FROM ( SELECT * FROM 表名 ORDER BY DBMS_RANDOM.VALUE )WHERE ROWNUM < = N;
oracle 中随机取一条记录的两种方法 V_COUNT INT:=0; V_NUM INT :=0; 1:TBL_MYTABLE 表中要有一个值连续且唯一的列FID BEGIN SELECT COUNT(*) INTO V_COUNT FROM TBL_MYTABLE; SELECT TRUNC(DBMS_RADOM.VALUE(1,V_COUNT+1)) INTO V_NUM FROM DUAL; SELECT * FROM TBL_MYTABLE T WHERE T.FID=V_NUM;
sql在不同数据库查询前几条数据 1. ORACLE SELECT * FROM TABLE_NAME WHERE ROWNUM <= N; HQL: from table_name t order by t.create_time desc where rownum<=n; 2. postgres查询前几条记录SQL SELECT * FROM TABLE_NAME LIMIT N; 3. DB2 SELECT * ROW_NUMBER() OVER(ORDER BY COL1 DESC
在使用sql语句进行前20条记录查询时SQL Server可以这样写: 1: select top 20 * from [table] order by ids desc 2: select top 20 * from [table] where id not in (select top 20 id from [table] ) 3: select top 20 * from (select top 30 * from [table] order by id) as tb1 order by
比如我数据库中存入的是一条一条的用户信息,现在想取出一个人的个人信息,并封装为Bean对象,可以使用queryForObject来获取数据并通过new BeanPropertyRowMapper(Bean.class)将数据转化为javaBean对象. 用法: queryForObject:查询并返回对象 new BeanPropertyRowMapper(Bean.class):是queryForObject的一个参数,将返回的对象封装为什么对象 public User findByid(in
项目需要筛选出不重复数据,以前没有做过,第一反应就是利用distinct处理,但是弄了好久也没搞出来,大家有知道的望告知下. 这次筛选没有使用distinct ,是利用group by ,利用id为唯一标示符(自增长),对按user进行排列,然后取重复项最小id(非重复项直接取唯一id),并以此id为条件查询,从而去除重复的数据. 数据格式为: 使用语句如下: select * from tbl_DPImg where ID in (select min(ID) from tbl_DPImg g
查看MYSQL数据库中所有用户 SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; 查看数据库中具体某个用户的权限 show grants for 'csexchange'@'%'; 解释:'csexchange'@'%'用户对pcp_reactor数据库下的mdm_user表有select权限,对pcp_reactor数据库下的cs_exchange表有select.inser
需要使用:分区函数用法(partition by 字段) select *,row_number() over(partition by item order by date ) as index from tab 分区索引 ------------------------------------------- SQL Server select * from (select * , row_number() over(partition by id order by state desc)