Combining the identifier of the parent and child into 2 tables and returning the result from another row

I have a basic understanding of SQL, so you need help and thanks in advance to those who respond.

I have 2 tables, one table contains headers and parameters that can be selected under these headers. Another table refers to actual references to the data in this table for a reference to the title and the title parameter.

I am trying to execute an SQL query to join these tables, and then refer to the parent / child identifier in one table in order to get the header + selected parameter from another table, but all I will return is the identification numbers. I created an image that should explain what I'm trying to get as a result .. and unsuccessfully!

This image will explain here:

http://i.imgur.com/hSPvY.jpg

NOTE. Above, where I say not 18 and 20, I can make my result show identification numbers .. but not the correct information from the name of the parent and the child’s title. (Server Support - Visiting Visited Sites)

This is where I came from SQL:

SELECT custom_def_organizations.title FROM custom_data_organizations INNER JOIN organizations ON custom_data_organizations.organization_id = organizations.id INNER JOIN custom_def_organizations ON custom_def_organizations.parent_id = custom_data_organizations.root_field_id AND custom_def_organizations.id = custom_data_organizations.field_id 
+4
source share
1 answer

The first query to join the parent and child, without custom_data_organization, but using the implied hierarchy:

 SELECT parent.id, child.id FROM custom_def_organizations AS parent JOIN custom_def_organizations AS child ON (child.parent_id = parent.id); 

This will return:

 18 19 18 20 18 21 18 22 18 23 

Now, to get other information:

 SELECT parent.id, child.id, CONCAT(parent.title, ' - ', child.title) AS title FROM custom_def_organizations AS parent JOIN custom_def_organizations AS child ON (child.parent_id = parent.id); 

This will return:

 18 19 Server Support - Yes 18 20 Server Support - Site Visits Chargeable 18 21 Server Support - Site Visits Included 18 22 ... 18 23 

Same concept, but with custom_data_organizations governing JOIN:

 SELECT cdo.id, CONCAT(parent.title, ' - ', child.title) AS title FROM custom_data_organizations AS cdo JOIN custom_def_organizations AS parent ON (cdo.root_field_id = parent.id) JOIN custom_def_organizations AS child ON (cdo.field_id = child.id); 

This will return:

  85 Server Support - Site Visits Chargeable ... 
+3
source

Source: https://habr.com/ru/post/1437867/


All Articles