Zeeshan Rang
Zeeshan Rang

Reputation: 19885

How can I fetch random rows from my mysql table

My Sql query is :

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%')

How can i make this query to fetch some Random row every-time it is fired.

Thanks Zeeshan

Upvotes: 0

Views: 83

Answers (3)

b7kich
b7kich

Reputation: 4431

Easy enough, 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()

See http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html for performance tips

Upvotes: 2

richo
richo

Reputation: 8989

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();

However this is a pretty terrible idea.

Upvotes: 0

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

ORDER BY RAND()

at the end of query. Not recommended.

Upvotes: 0

Related Questions