Answer the question
In order to leave comments, you need to log in
MySQL 5.7 and SELECT DISTINCT JSON for each attribute?
Hello, how to get unique occurrences for each attribute?
For example,
1. {["name":"name1"]};
2. {["name":"name2", "name1"]};
3. {["name":"name1"]};
SELECT DISTINCT features->"$.name" FROM data
Answer the question
In order to leave comments, you need to log in
LIMIT 1?
UPDATE:
You can do it with an inline procedure:
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$$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question