DELIMITER $$
DROP PROCEDURE IF EXISTS extract_params;
CREATE PROCEDURE extract_params()
BEGIN
DECLARE exit_flag INT DEFAULT 0;
DECLARE j JSON;
DECLARE i INT;
DECLARE q VARCHAR(255);
DECLARE t VARCHAR(255);
DECLARE c CURSOR FOR
SELECT CAST(jdoc->'$."name"' AS JSON) FROM t1;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET exit_flag = 1;
DROP TEMPORARY TABLE IF EXISTS tmp_123;
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_123 (jsn VARCHAR(255)) ENGINE=MEMORY;
OPEN c;
fetch_loop: LOOP
FETCH c INTO j;
IF exit_flag THEN LEAVE fetch_loop; END IF;
IF JSON_TYPE(j) = 'ARRAY' THEN
SET i = JSON_LENGTH(j);
WHILE i > 0 DO
SET i = i - 1;
SET q = CONCAT('$[',i,']');
SET t = JSON_UNQUOTE(JSON_EXTRACT(j, q));
INSERT INTO tmp_123 VALUES(t);
END WHILE;
END IF;
END LOOP;
CLOSE c;
SELECT DISTINCT(jsn) FROM tmp_123;
END$$
select * from users where id in (
select f1 from (
select friends.friend1 as f1 from friends where friends.friend2 = 1
UNION ALL
select friends.friend2 from friends where friends.friend1 = 1) as s1
where f1 in (
select friends.friend1 from friends where friends.friend2 = 49519
UNION ALL
select friends.friend2 from friends where friends.friend1 = 49519
) )