0

I have problem querying table with variable in IN function.

SELECT 
    s.date,
    (SELECT
         GROUP_CONCAT(value) 
         FROM value 
         WHERE id_value 
         IN(s.ref_values)
    ) AS vals
FROM stats s
ORDER BY s.date DESC
LIMIT 1

Where s.ref_values is '12,22,54,15'. I get only one return for first number (12).

When I insert that value directly in IN(12,22,54,15) it finds all 4.

So, there must be problem with using variable in IN. What am I doing wrong?

0

1 Answer 1

1

Rewrite your query to

SELECT 
s.date,
(SELECT
     GROUP_CONCAT(value) 
     FROM value 
     WHERE id_value 
     IN(SELECT ref_values FROM stats)
) 
AS vals
FROM stats s
ORDER BY s.date DESC
LIMIT 1

and see if that helps.

You should ideally be passing a result set as a parameter to IN

Sign up to request clarification or add additional context in comments.

2 Comments

Same result :-(. I can get extra (first) query for ref_values, but it is complicated when I want to have it for each row.
Still facing the same problem?

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.