0

It's working but returning just one Record. I Have 5 in Total.

Catalog.similar is json type field. I remove [""] from the return and putting inside the WHERE IN Condition. But I get just one record.

SELECT * FROM similar_codes a WHERE a.id IN (SELECT REPLACE(SUBSTR(c.similar,2,LENGTH(c.similar)-2),'"','') FROM catalog c WHERE c.id=1)

The Return from the select inside the WHERE IN is.

SELECT REPLACE(SUBSTR(c.similar,2,LENGTH(c.similar)-2),'"','') FROM catalog c WHERE c.id=1)

3380, 3382, 1952, 3384, 3383
3
  • remove where codition with id Commented May 15, 2017 at 12:33
  • it's always bad idea to save list in filed. You need to create similar table which will be linked to catalog. Then just use simple join. Commented May 15, 2017 at 12:34
  • i trying to use json field... it's a bad idea json at all ? Commented May 15, 2017 at 12:57

2 Answers 2

2

Rather than trying to develop your own way to search JSON, why not use the built in functions? Alternatively, if you are using a version of MySQL that doesn't support JSON data, you might be better looking at using a linked table.

-- Create main table
CREATE TEMPORARY TABLE test (id INT(11));
INSERT INTO test (id) VALUES (3380), (3382), (1952), (3384), (3383);

-- Create JSON table
CREATE TEMPORARY TABLE test_search (id INT(11), similar JSON);
INSERT INTO test_search (id, similar) VALUES (1, JSON_ARRAY(3380, 3382, 1952, 3384, 3383));

-- Select
SELECT t.id 
FROM (SELECT similar AS ids FROM test_search WHERE id = 1) ts
INNER JOIN test t ON JSON_CONTAINS(ts.ids, CAST(t.id AS json), '$');
Sign up to request clarification or add additional context in comments.

2 Comments

Thank's. It's Worked
Precise answer!
0

You can use GROUP_CONCAT()

SELECT * FROM similar_codes a WHERE a.id IN 
(SELECT GROUP_CONCAT(REPLACE(SUBSTR(c.similar,2,LENGTH(c.similar)-2),'"','')) FROM catalog c WHERE c.id=1)

3 Comments

keep returning one record. the first 3380
And what does this query return? SELECT GROUP_CONCAT(REPLACE(SUBSTR(c.similar,2,LENGTH(c.similar)-2),'"','')) FROM catalog c WHERE c.id=1;
Get all similars. ["3380", "3382", "1952", "3384", "3383"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.