MySQL gets all the parent or sub-categorical query results for a category ID

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.