mysql - Select An Optional Parent Row Using Same Table -


I am trying to select all rows from a table and in the same line about their parents The information also includes if they have a parent otherwise the original data should be blank.

  SELECT p.id, p.name, '0' AS Page_xist page from page P LEFT JOIN pages PP ON (PP) .page_id = p.parent) WHERE p.page_type = 3 ORDER I want a successful match for parents to add the following two columns to each line: "parent_id" and "parent_name"  

The end result of a page with parents looks like this:

  id = 2 name = "test" page_value = 0 parent_id = 1 parent_name = "abc"   

If the page does not have any parent, then Should look like this:

  id = 3 name = "other page" page_value = 0 parent_id = 0 parent_name = ""    

As you do LEFT JOIN in your code, use the nickname PP for parental information F, Code> If no parent is present

In this way, the following questions will include fields from parents:

  SELECT p.id, P.name, '0' AS Page_exists, pp.page_id as parent_id, pp, name as parent_name Scope page as LEFT JOIN pages PP (PP. Page_id = p.parent) WHERE p.page_type = 3 ORDER BY Name ASC   

0 and "instead of NULL s when a parent does not exist , Then you can use the case in statement SELECT section:

  SELECT pp.id, pp.name, '0' As the page_exists, case pp page_ id then pp. Parent_id as the page_ID ELSE 0 END, in case the PPN name is named PPName ELSE 'parent_name    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -