mymotherland
mymotherland

Reputation: 8226

How to get random rows in inner join using mysql?

I am using following queries to fetch random rows,

'SELECT * FROM TABLENAME1 ORDER BY RAND() LIMIT 1' . this query will return random rows.But i need to get the name which is placed in another table.so here i have to use join queries.

'SELECT tablename1.*,tablename2.name FROM tablename1 INNER JOIN tablename2 ON tablename1.id = tablename2.id ORDER BY RAND(tablename1.id)'

The above queries return same rows everytime, I am not getting random rows.Kindly help me on this.

Upvotes: 0

Views: 1134

Answers (2)

Widor
Widor

Reputation: 13275

For some reason, you're seeding the 2nd version of the query with tablename1.id. Calls to RAND() with the same seed value return the same results.

Upvotes: 1

hughes
hughes

Reputation: 5713

RAND(tablename1.id) uses tablename1.id as the seed for the pseudorandom number generator, so it will give you the same result every time. Try just using RAND() in your second query.

Upvotes: 1

Related Questions