[MDEV-23314] strange/random select results Created: 2020-07-28  Updated: 2020-08-05  Resolved: 2020-08-05

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Subquery
Affects Version/s: 5.5, 10.2.32, 10.3.23, 10.4.13
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Miguel Dias Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: None


 Description   

Strange select results when running this query "select * FROM tmp WHERE id in (select cast(rand()*10 as int)+1);" against a simple small table.

This is easy enough to reproduce:

CREATE TABLE `tmp` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_user` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

INSERT INTO `tmp` VALUES (1,80),(2,253),(3,333),(4,569),(5,606),(6,618),(7,660),(8,663),(9,690),(10,693);

select * FROM tmp WHERE id in (select cast(rand()*10 as int)+1);
Empty set (0.001 sec)

select * FROM tmp WHERE id in (select cast(rand()*10 as int)+1);
-----------+

id id_user

-----------+

5 606
9 690

-----------+
2 rows in set (0.001 sec)
select * FROM tmp WHERE id in (select cast(rand()*10 as int)+1);
-----------+

id id_user

-----------+

1 80
4 569
8 663
9 690

-----------+
4 rows in set (0.000 sec)

but when I run select cast(rand()*10 as int)+1 alone, I only ever get one result...



 Comments   
Comment by Elena Stepanova [ 2020-08-05 ]

The value is recalculated for every row, and on each recalculation rand() returns a different result, hence the randomness.

Generated at Thu Feb 08 09:21:28 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.