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. The end result of a page with parents looks like this: If the page does not have any parent, then Should look like this: As you do In this way, the following questions will include fields from parents: 0 and "instead of
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"
id = 2 name = "test" page_value = 0 parent_id = 1 parent_name = "abc"
id = 3 name = "other page" page_value = 0 parent_id = 0 parent_name = ""
LEFT JOIN in your code, use the nickname PP for parental information F, > Code> If no parent is present
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
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
Post a Comment