MySQL Forums
Forum List  »  General

Subquery's rand() column re-evaluated for every repeated selection in MySQL 5.7 vs MySQL 5.6
Posted by: Pavel Sharov
Date: June 07, 2017 02:54AM

I am doing a subselect in which I have a column involving random number generation. In the main query I select this column twice. In MySQL 5.6 it works as I expect, the rand() value being fixed. The 5.7 execution looks like rand() is re-evaluated individually for each selection. Is this correct behavior? What should I do in 5.7 to make it work just as in 5.6? I posted this question on StackOverflow (https://stackoverflow.com/questions/44336391/subquerys-rand-column-re-evaluated-for-every-repeated-selection-in-mysql-5-7), there's an opinion in comments there that it is a bug.

CREATE TABLE t (
`id` BIGINT(20) NOT NULL PRIMARY KEY AUTO_INCREMENT
) ENGINE=InnoDB;

insert into t values();
insert into t values();
insert into t values();
insert into t values();
insert into t values();

SELECT q.i, q.r, q.r
FROM (
SELECT
id AS i,
(FLOOR(RAND(100) * 4)) AS r
FROM t
) q;

MySQL 5.6 yields (values are the same):

+---+-----+-----+
| i | r | r |
+---+-----+-----+
| 1 | 0 | 0 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 2 | 2 |
| 5 | 1 | 1 |
+---+-----+-----+

while 5.7 yields (values are different):

+---+-----+-----+
| i | r | r |
+---+-----+-----+
| 1 | 0 | 2 |
| 2 | 3 | 2 |
| 3 | 1 | 1 |
| 4 | 2 | 1 |
| 5 | 2 | 0 |
+---+-----+-----+

Options: ReplyQuote


Subject
Written By
Posted
Subquery's rand() column re-evaluated for every repeated selection in MySQL 5.7 vs MySQL 5.6
June 07, 2017 02:54AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.