mysql where columnname in (function(a value)) not working -
i have strange situation here mysql query: when where unterkategorie in (children_csv(1))
used there no result. second "where unterkategorie in (11,12,13,28,29,32,14,15,16,30,31,33,34,35)"
fetching records when substitute function name results of function when executed separately
the full query is:
select k.name category_name, p.unterkategorie, p.artikelnummer, p.hauptkategorie, p.id, p.name product_name, p.preis, p.sortierung, p.verpackungseinheit produkte p, kategorie k unterkategorie in (children_csv(1)) unterkategorie in (11,12,13,28,29,32,14,15,16,30,31,33,34,35) , p.unterkategorie = k.id order unterkategorie, p.sortierung
following function definition
delimiter // create definer=`root`@`localhost` function `children_csv`(child int)returns varchar(1000) charset utf8 begin declare return_value varchar(1000); select group_concat(level separator ',')childrens return_value ( select @ids := ( select group_concat(`id` separator ',') `kategorie` find_in_set(`parent`, @ids) order parent, sortierung ) level `kategorie` join (select @ids := child) temp1 find_in_set(`parent`, @ids) ) temp2; return return_value; end; // delimiter ;
your function returning single value, string. not returning a list of values (because mysql functions not that). if want use function directly, can use find_in_set()
:
where find_in_set(unterkategorie, children_csv(1))
i caution mysql cannot use index on unterkategorie
, might slower.
if want faster query, can construct query string (called dynamic sql) , use prepare
, exec
run it.
if coming programming language, need learn functions not route better performance in sql. moving logic function not performance.
Comments
Post a Comment