Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why am I getting multiple rows in MYSQL query?

I am trying to retrive a random row from a table (user), by using the Primary Key _id and the following query.

SELECT * 
FROM   user 
WHERE  _id IN (SELECT Floor(1 + ( Rand() * ( Count(_id) - 1 ) )) 
               FROM   user); 

No rows have been deleted from the table but I (inconsistently) get multiple rows returned...

I find this amazing, as any result from the subquery should surely return one integer, which against a primary key.....should always only be 1 record!

I want to use one query, I will probably make it a prepared statement, and I am not using the LIMIT clause because I cannot use @variables to pick the random row.

My MySQL welcome statement tells me my version is as follows: Server version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)

like image 296
user2149048 Avatar asked Dec 07 '25 19:12

user2149048


1 Answers

How about using ORDER BY RAND() LIMIT 1; in your query instead of where _id IN (select FLOOR(1+(RAND()*(COUNT(_id)-1))) from user);

You get multiple rows cause (select FLOOR(1+(RAND()*(COUNT(_id)-1))) from user) will return you a different value for ever record in 'user' table. That is because of the RAND bit.

Run select FLOOR(1+(RAND()*(COUNT(_id)-1))) from user on its own and you will see.

like image 153
Yasen Zhelev Avatar answered Dec 09 '25 14:12

Yasen Zhelev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!