2

This command works:

SET @a = '1,2,3,4,5,6,7,8,9';
SELECT Id FROM MyTable2 WHERE find_in_set(Id, @a)

I would like to know if it's possible to use a SELECT instead of '1,2,3,4,5,6,7,8,9' because I wasn't able to do it.

I've tryed:

SET @a = CONCAT_WS(',', (SELECT Id FROM MyTable1 WHERE Id < 10));

but I get error:

Subquery returns more than 1 row

Is there a way to do what I'm trying?

3
  • I can't think why you'd want to do this. Commented Apr 7, 2016 at 14:22
  • @Strawberry I need to use the value in 3 queries so if I have it into a variable I'll avoid 3 subqueries Commented Apr 7, 2016 at 14:28
  • Hm, that's not a very convincng answer. Commented Apr 7, 2016 at 14:29

1 Answer 1

2

Try this way:

SET @a = (SELECT GROUP_CONCAT(Id) FROM mytable WHERE Id < 10);

This will set @a to a string like '1, 2, 3, ...'.

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

Comments

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.