如何从mysql表中获取随机行

时间:2010-12-23 23:17:35

标签: mysql

我的Sql查询是:

SELECT g.name, g.description, g.hash, g.views FROM c7_groups g, c7_groups_affiliations ga, c7_groups_categories gc WHERE ga.groups_hash = g.hash AND ga.groups_categories_id = gc.id AND g.groups_categories_id = 611
AND LOWER(ga.name) LIKE LOWER('%$school_name%')

如何在每次触发时使此查询获取一些Random行。

由于 Zeeshan

3 个答案:

答案 0 :(得分:2)

很简单,ORDER BY RAND():

  

SELECT g.name,g.description,g.hash,g.views FROM c7_groups g,c7_groups_affiliations ga,c7_groups_categories gc WHERE ga.groups_hash = g.hash AND ga.groups_categories_id = gc.id AND g.groups_categories_id = 611   AND LOWER(ga.name)LIKE LOWER('%$ school_name%')ORDER BY RAND()

有关效果提示,请参阅http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

答案 1 :(得分:0)

  

ORDER BY RAND()

在查询结束时。不推荐。

答案 2 :(得分:0)

SELECT g.name, g.description, g.hash, g.views FROM c7_groups g, c7_groups_affiliations ga, c7_groups_categories gc WHERE ga.groups_hash = g.hash AND ga.groups_categories_id = gc.id AND g.groups_categories_id = 611
AND LOWER(ga.name) LIKE LOWER('%$school_name%') ORDER BY RAND();

然而,这是一个非常可怕的想法。