Description:
Blank set or doubled when sampled by a random unique identifier
The result of the queries:
mysql> drop table if exists test;
Query OK, 0 rows affected (0.00 sec)
mysql> create table test (
-> id int not null auto_increment,
-> dsc char(12),
-> constraint pk_id primary key(id)
-> ) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test(dsc) values ('one'), ('two'), ('three'), ('four'), ('five');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from test;
+----+-------+
| id | dsc |
+----+-------+
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
| 5 | five |
+----+-------+
5 rows in set (0.00 sec)
mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
mysql> select floor(1 + rand() * 5) id;
+----+
| id |
+----+
| 5 |
+----+
1 row in set (0.00 sec)
mysql> select id, dsc from test where id = (select floor(1 + rand() * 5) id);
Empty set (0.00 sec) (***** Why? *****)
mysql> select floor(1 + rand() * 5) id;
+----+
| id |
+----+
| 4 |
+----+
1 row in set (0.00 sec)
mysql> select id, dsc from test where id = (select floor(1 + rand() * 5) id);
Empty set (0.00 sec) (***** Why? *****)
mysql> select floor(1 + rand() * 5) id;
+----+
| id |
+----+
| 3 |
+----+
1 row in set (0.00 sec)
mysql> select id, dsc from test where id = (select floor(1 + rand() * 5) id);
+----+------+
| id | dsc |
+----+------+
| 4 | four |
+----+------+
1 row in set (0.00 sec)
mysql> select floor(1 + rand() * 5) id;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)
mysql> select id, dsc from test where id = (select floor(1 + rand() * 5) id);
+----+------+
| id | dsc |
+----+------+
| 4 | four |
| 5 | five |
+----+------+
2 rows in set (0.00 sec) (***** Why? *****)
mysql> select floor(1 + rand() * 5) id;
+----+
| id |
+----+
| 5 |
+----+
1 row in set (0.00 sec)
mysql> select id, dsc from test where id = (select floor(1 + rand() * 5) id);
+----+------+
| id | dsc |
+----+------+
| 4 | four |
| 5 | five |
+----+------+
2 rows in set (0.00 sec) (***** Why? *****)
How to repeat:
drop table if exists test;
create table test (
id int not null auto_increment,
dsc char(12),
constraint pkGood_id primary key(id)
) engine=myisam;
insert into test(dsc) values ('one'), ('two'), ('three'), ('four'), ('five');
select * from test;
select count(*) from test;
select floor(1 + rand() * 5) id;
select id, dsc from test where id = (select floor(1 + rand() * 5) id);
select floor(1 + rand() * 5) id;
select id, dsc from test where id = (select floor(1 + rand() * 5) id);
select floor(1 + rand() * 5) id;
select id, dsc from test where id = (select floor(1 + rand() * 5) id);
select floor(1 + rand() * 5) id;
select id, dsc from test where id = (select floor(1 + rand() * 5) id);
select floor(1 + rand() * 5) id;
select id, dsc from test where id = (select floor(1 + rand() * 5) id);
Suggested fix:
no