0

I am using following query to update all the children of particular topic.

UPDATE topics SET reuse = 0 WHERE topic_id IN (SELECT GetChildTopics(187));

Where

SELECT GetChildTopics(187);

returns "188,190,189" but my update query is updating only first row with topic_id = 188, instead of updating first topic only, it should update all 3 topics.

When I put the values manually it works fine.

UPDATE topics SET reuse = 0 WHERE topic_id IN (188,190,189);

Can anyone suggest what's wrong I am doing here?

Here is the code for GetChildTopics MySQL Function

    CREATE DEFINER=`root`@`localhost` FUNCTION `GetAncestry`(GivenID INT) RETURNS varchar(1024) CHARSET latin1
    DETERMINISTIC
BEGIN
    DECLARE rv VARCHAR(1024);
    DECLARE cm CHAR(1);
    DECLARE ch INT;

    SET rv = '';
    SET cm = '';
    SET ch = GivenID;
    WHILE ch > 0 DO
        SELECT IFNULL(parent_topic_id,-1) INTO ch FROM
        (SELECT parent_topic_id FROM topic_list WHERE id = ch) A;
        IF ch > 0 THEN
            SET rv = CONCAT(rv,cm,ch);
            SET cm = ',';
        END IF;
    END WHILE;
    RETURN rv;
END
4
  • Returns the function the string '188,190,189' or a resultset with 3 records? Commented Jun 28, 2016 at 9:19
  • 1
    Better share your GetChildTopics function Commented Jun 28, 2016 at 9:20
  • @Frank function returns a string with comma separated values Commented Jun 28, 2016 at 9:20
  • Thats the problem. It must return a result set in this context Commented Jun 28, 2016 at 19:10

1 Answer 1

3

Try this;)

UPDATE topics SET reuse = 0 WHERE FIND_IN_SET(topic_id, GetChildTopics(187));
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.