数据库为SQL SERVER,从上万条数据中,如何随机获取1000数据,并且加上查询条件;
这是我写的,可是不知道该怎么加上查询条件.忘高手赐教。
select top 10 * from baseinfo where 
tel like 
char(48+abs(checksum(newid())%10))+'%'+
char(48+abs(checksum(newid())%10))+'%'+
char(48+abs(checksum(newid())%10))+'%'+
char(48+abs(checksum(newid())%10))+'%'

解决方案 »

  1.   

    select top 10 * from baseinfo order by newid()
      

  2.   

    select top 10 * from baseinfo where .... order by newid()
      

  3.   

    如何加入查询条件呢?
    比如
    select top 10 * from baseinfo where name='张三' order by newid()
    这样写会出错,如何不出错呢?
      

  4.   

    SQL Server 使用Order by newID()返回随机数据
     
    SQL Server 使用Order by newID()返回随机数据 
    SQLServer提供函数newid,可以随机抽取数据,举例如下
    select top 100 * 
    from tableA
    上面这条语句得到的数据是前100返回结果,一般是比较集中在某一天或者某个区域。
    select top 100 * 
    from tableA 
    group by newId()
    上面这条语句得到的数据数据库随即抽取的100条结果,是很好的抽样数据 
      

  5.   

    select top 10 * from baseinfo where name='张三' group by newid()
      

  6.   


    use tempdb
    if object_id('tb') is not null drop table tb
    go
    create table tb([b] varchar(50),[c] varchar(50),[d] INT)
    insert into tb
    select '20081201','092152',15 union all
    select '20081201','092352',16 union all
    select '20081201','092632',11 union all
    select '20081201','096158',18 union all
    select '20081201','102130',15 union all
    select '20081202','092152',12 union all
    select '20081202','092342',13 union all
    select '20081202','093522',20 union all
    select '20081202','103652',15 union all
    select '20081204','092232',17 union all
    select '20081204','092552',19 union all
    select '20081204','095552',15 
    go
    select top 10 * from tb where b='20081201' order by newid()
    /*
    b                                                  c                                                  d
    -------------------------------------------------- -------------------------------------------------- -----------
    20081201                                           092632                                             11
    20081201                                           102130                                             15
    20081201                                           092352                                             16
    20081201                                           092152                                             15
    20081201                                           096158                                             18(5 行受影响)
    */出什么错呢,测试OK啊
      

  7.   

    select top 10 * from baseinfo where pname='朱冬冬' order by newid()这样的查询操作,在对上万条或百万条数据进行操作的时候,会不会影响查询速度?
      

  8.   

    百万的记录,要随机,速度不可能快得起来,这是鱼和熊掌的故事.如果想快:
    1.建立索引.
    2.提高硬件配置.
    3.优化查询.(不过,随机查询就不是很好的查询.)
    http://topic.csdn.net/u/20080722/21/4501e9cb-a9fa-437e-aaf6-7b629bd26215.html?seed=1110065108
    http://topic.csdn.net/u/20080716/11/2317d040-48e7-42da-822e-040b4c55b46d.html
      

  9.   

    select top 1000 * from baseinfo where col1='' order by newid()
      

  10.   

    试试这个。不了解可查看联机帮助SELECT TOP 10 * FROM dbo.baseinfo TABLESAMPLE SYSTEM(30 PERCENT)
    WHERE  1=1
      

  11.   

    group by newId() 就可以