根据一个条件在数据库中查出N条数据后,如何随机抽取其中的一条……

解决方案 »

  1.   

    select * from table1 where col=123 order by rand() limit 1
      

  2.   

    在mysql中 
    你的随机请求时通过函数 rand()实现 
    取一条 是通过Limit来限制 具体写法 1L给出了
      

  3.   

    1楼正解,但是用rand效率会大大降低,尤其是当表中有很多条数据的时候,查询会相当慢!
    既然是获取随机一条,那你可以先用程序获取随机的范围来替代rand
    例如
    一个表中有100W条数据(字段:id,uid,content),每次查询uid为58的随机的10条数据。
    首先第一步:用程序来获取一个随机数。
    $id=rand(1,1000000); 
    select id,uid,content from Table where uid=58 and id>'$id' limit 1
    这样的话我们写sql语句不仅能实现了功能,还保证了效率http://www.yirenju.net 网络小游戏
      

  4.   

    看下这个
    http://blog.csdn.net/dainiao01/archive/2008/09/02/2865383.aspx1楼的效率确实低。
      

  5.   

    给你一个方法:
    1.给表加个随机列
    alter table tbName add colName double;
    2.给列加索引
    create index indexName on tbName(colName);
    3.给列加个随机值
    update tbName set colName=rand();
    4.接着你就可以使用啦
    select top 10 * from tbName where colName>rand() order by colName limit 1
      

  6.   

    1281432257-ProjectPromise.response_time*3600-BxdIw.sub_time)/60 as overtime每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分