Sanky
Sanky

Reputation: 397

How to execute SELECT from List SQL query inside the MYSQL function?

I have created a function in MYSQL as follow

CREATE FUNCTION `getTaskIds` ( typeIds TEXT) RETURNS TEXT
BEGIN
DECLARE output TEXT;
SELECT GROUP_CONCAT(id) INTO output FROM task WHERE task_id IN (typeIds );
RETURN output;
END

when I execute the function like SELECT getTaskIds( '1,2,3' ), It gives me the result for only task_id = 1. It does not condider task_id 2 and 3.

What am I missing in the function? Can you guys help me out?

Thanx in advance.

Upvotes: 0

Views: 1251

Answers (2)

Dan
Dan

Reputation: 876

You can't use IN since you are passing in a string. Try this FIND_IN_SET():

 SELECT GROUP_CONCAT(id) INTO output FROM task WHERE FIND_IN_SET(task_id, typeIds) > 0;

Upvotes: 2

Refilon
Refilon

Reputation: 3489

You need to pass all parameters in the function. So if you want 3 parameters:

CREATE FUNCTION `getTaskIds` ( typeId1 TEXT, typeId2 TEXT, typeId3 TEXT) RETURNS TEXT
BEGIN
DECLARE output TEXT;
// do what you want to do
RETURN output;
END

Upvotes: 0

Related Questions