Hierarchical queries in Oracle are replaced with MySQL

Source: Internet
Author: User

Oracle's start with ... Connect by implements a recursive query for the tree, but now requires MySQL to implement the same recursive query tree function. This function is I have never used before, so I surf the internet and find some information began to do up.

The original Oracle statement is

Select ' | ' | | c.seq_cate| | ' | '

from Osr_category C

start with c.seq_cate = #serviceCategory #

Connect by Prior c.seq_cate = c.parent_id)

MySQL does not have a corresponding method to implement the function of recursive query tree, so we have to write a function according to the internet to achieve:

CREATE FUNCTION getchildlist (Rootid VARCHAR (1000))

RETURNS VARCHAR (1000)

BEGIN

DECLARE ptemp VARCHAR (1000);

DECLARE ctemp VARCHAR (1000);

SET ptemp= ' $ ';

SET Ctemp=rootid;

While ctemp are NOT NULL does

Set Ptemp=concat (ptemp, ', ', ctemp);

SELECT Group_concat (seq_cate) to ctemp from Osr_category

WHERE Find_in_set (parent_id,ctemp) >0;

END while;

RETURN ptemp;

END

then its SQL language The sentence should read:

Select ' | ' | | c.seq_cate| | ' | '

from Osr_category C

where Find_in_set (C.seq_cate, Getchildlist (#serviceCategory #))



Hierarchical queries in Oracle are replaced with MySQL

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.