S
S
swcalc2016-03-04 16:33:18
MySQL
swcalc, 2016-03-04 16:33:18

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
It will return 1 and 2 rows, but I need to check all the attributes, and if 1 of them has already been met before, then exclude it from the output, that is, get only the 1st row for this request , is this possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2016-03-04
@swcalc

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 question

Ask a Question

731 491 924 answers to any question