当前位置: 代码迷 >> Sql Server >> 请问下关于一个数据库随机的有关问题
  详细解决方案

请问下关于一个数据库随机的有关问题

热度:55   发布时间:2016-04-24 09:40:28.0
请教下关于一个数据库随机的问题。
比如我有1 3 5 7 9 这几个值,我怎么随机取里面的一个值呢,求大神指导下
------解决思路----------------------
----------------------------------------------------------------
-- Author  :DBA_HuangZJ(發糞塗牆)
-- Date    :2014-11-21 10:10:35
-- Version:
--      Microsoft SQL Server 2014 (CTP1) - 11.0.9120.5 (X64) 
-- Jun 10 2013 20:09:10 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([a] int)
insert [A]
select 1 union all
select 3 union all
select 5 union all
select 7 union all
select 9
--------------开始查询--------------------------

select top 1 * 
from [A]
order by checksum(newid())
----------------结果----------------------------
/* 
a
-----------
5

*/

------解决思路----------------------
select cast(rand()*5 as int)*2+1
  相关解决方案