I have two tables that look like this:
Table: matrix
matrix_id round_id p_1_1 p_1_2 p_2_1 p_2_2 p_3_1 p_3_2
INT(11) INT(11) INT(11) INT(11) INT(11) INT(11) INT(11) INT(11)
AUTOINCREMENT
Table: matrix_elements:
element_id matrix
INT(11) INT(11)
AUTOINCREMENT
Now I need to randomly SELECT from the table matrix_elements
and INSERT
a new row into the table matrix
. Each of those p_1_1, p_1_2 etc.. will have to be filled with the random matrix
from the matrix_elements
table.
The only requirement is that each two of those columns p_1_1 etc.. will have to be filled with the value: 8
I am totally lost and I did not try anything before you ask. I am just in need of some good directions in order to achieve this, not necessarly a prepared code.
Thanks for any suggestion.
This takes multiple statements to do one row... and I may have misunderstood your requirements, but here goes:
EDIT: Modified to be one statement and actually work with RAND()
INSERT INTO matrix
SELECT
NULL,
1, -- NO idea what round_id should be...
IF( 0 in (one8, two8), 8, (SELECT element_id
FROM matrix_elements ORDER BY RAND() LIMIT 1)),
IF( 1 in (one8, two8), 8, (SELECT element_id
FROM matrix_elements ORDER BY RAND() LIMIT 1)),
IF( 2 in (one8, two8), 8, (SELECT element_id
FROM matrix_elements ORDER BY RAND() LIMIT 1)),
IF( 3 in (one8, two8), 8, (SELECT element_id
FROM matrix_elements ORDER BY RAND() LIMIT 1)),
IF( 4 in (one8, two8), 8, (SELECT element_id
FROM matrix_elements ORDER BY RAND() LIMIT 1)),
IF( 5 in (one8, two8), 8, (SELECT element_id
FROM matrix_elements ORDER BY RAND() LIMIT 1))
FROM (
SELECT
one8,
IF( one8 = two8, two8 + 1, two8 ) as two8
FROM (
SELECT
FLOOR(RAND() * 6) AS one8,
FLOOR(RAND() * 5) AS two8
) AS a
) AS b;