Create a Custom Function:
1 DELIMITER $$2 3 Use ' dressv_website ' $$4 5 DROP FUNCTION IF EXISTS ' fn_queryrelation ' $$6 7CREATE definer= ' sa ' @ '% ' FUNCTION ' fn_queryrelation ' (Areaid int,relation TINYINT (1)) RETURNS VARCHAR (4000) CHARSET UTF88 BEGIN9DECLARE stemp VARCHAR (4000);TenDECLARE Stempchd VARCHAR (4000); OneSET stemp ='$'; ASET Stempchd =CAST (Areaid as CHAR); -IF relation=0 Then - While stempchd are not NULL does theSET stemp = CONCAT (Stemp,',', stempchd); -SELECT Group_concat (ParentID) to Stempchd from Product_leimu WHERE find_in_set (ID,STEMPCHD) >0; - END while; - ELSE + While stempchd are not NULL does -SET stemp = CONCAT (Stemp,',', stempchd); +SELECT Group_concat (ID) into Stempchd from Product_leimu WHERE find_in_set (PARENTID,STEMPCHD) >0; A END while; at END IF; - RETURN stemp; - end$$ - -DELIMITER;
fn_queryrelation
Query statement:
1 SELECT * from Product_leimu WHERE find_in_set (ID, fn_queryrelation (One,0
MYSQL
MySQL gets all the parent or sub-categorical query results for a category ID