thank you for reading this question.
Im trying to write a SQL statement that should be able to retrieve certain random elements from a table, exactly X elements from all IDs appearing in a set of IDs. Im a MySQL newbie, so the way I am trying to solve this may not be the correct one - in that case, please guide me.
Right now I have the following table:
+-------------+------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------+------+-----+---------+----------------+
| id0 | int | NO | PRI | NULL | auto_increment |
| id1 | int | YES | MUL | NULL | |
| id2 | int | YES | | NULL | |
+-------------+------+------+-----+---------+----------------+
id0 and id2 are unique, and for every id1 we can have multiple id2 related.
My goal is to be able to feed MySQL a set of id1 values, and for each one, I need to retrieve X number of random elements that pertain to that ID (a list of id2 values related to that id1, list is of length X).
I've been reading about windows and using functions with them, so I created a custom function:
DELIMITER //
CREATE FUNCTION get_x_random (
X INT,
id1_in INT
) RETURNS INT
BEGIN
DECLARE id2_return INT;
SELECT id2 INTO id2_return
FROM table
WHERE id1 = id1_in
ORDER BY RAND() LIMIT X;
RETURN id2_return;
END; //
And I am trying to use this function with the following MySQL statement:
SELECT
get_x_random(1, id1) OVER w AS x
FROM table
WHERE id1 IN (
SELECT t2.id1
FROM table AS t2
WHERE t2.id1 IN SET_OF_VALUES
GROUP BY t2.id1
)
WINDOW w AS (PARTITION BY id1);
However, this is not working (Syntax error near OVER w AS ....)
What am I doing wrong? Is it even possible to use custom functions along with windows? Is there any other possible approach to tackle this problem?
Note: I tried iterating with the Python MySQL wrapper over the different id1's doing a simple
SELECT id2
FROM table
WHERE id1 = {str(id1)}
ORDER BY RAND() LIMIT {int(X)}
And works, however, the accumulated fetching of all results makes it too slow (the length of the list of ids is in the order of hundreds), so I would like to find a purely SQL solution.
Thank you so much for reading until this point.