Hi
I have created a function called GetContentCategories which gets a list of categories that are linked to a content record.
For example, if I run the following SQL:
SELECT *,GetContentCategories(contentId) from content
where '2' in (GetContentCategories(contentId))
As you can see, I am returning all content items that have a categoryList that contains category 2.
So far so good.
If I try and run that SQL when there is only ONE category returned by GetContentCategories(contentId) then this query works fine.
For example:
SELECT *,GetContentCategories(contentId) from content
where 2 in (2)
But, if there are more than one categories linked to the content item and more than one category id is returned by calling GetContentCategories(contentId), then the query returns no records. For example:
SELECT *,GetContentCategories(contentId) from content
where 2 in (2,3,6)
As you can see, there are 3 integer values returned by the function. 2 is clearly in the list, yet the query returns no records??
BUT - and here's the strange part, if i manually type in the values for the IN statement, then it works. In other words:
WORKS:
SELECT *,GetContentCategories(contentId) from content
where 2 in (2,3,6)
DOESN'T WORK:
SELECT *,GetContentCategories(contentId) from content
where 2 in (GetContentCategories(contentId))
(where GetContentCategories(contentId) returns 2,3,6
What am I missing here. Manually typing in the values works, but using a function to generate the values does not. Yet the value returned by the function is EXACTLY the same as that typed manually - I have double checked!
CREATE DEFINER=`root`@`%` FUNCTION `GetContentCategories`(in_contentId BIGINT) RETURNS varchar(1024) CHARSET latin1
BEGIN
DECLARE out_categoryList VARCHAR(1024);
SET out_categoryList = (SELECT (CAST(GROUP_CONCAT(categoryId) AS CHAR(10000) CHARACTER SET utf8)) AS contentCategories FROM contentToCategory where contentId = in_contentId);
RETURN out_categoryList;
END
1:
2:
3:
4:
5:
6:
Select allOpen in new window
by: nemws1Posted on 2009-08-26 at 12:58:31ID: 25191595
I would think what you have would work as well.
I'm not duplicating what you're getting exactly (I'm getting *one* row back, not zero), but it still isn't working the way I would think.
I'm still working on it, though. ;-)