skip to Main Content

I have table look like this in phpmyadmin:

+-----------+-----------------+-------------------+
|nom_ele_id | nom_ele_title   | nom_ele_parent_id | 
+===========+=================+===================+
| 7060      | STANDARD        |   NULL            |  
| 6953      | SOL             |   7060            | 
| 6957      | type de sol     |   6953            |
+-----------+-----------------+-------------------+

I would like to have "nom_ele_tile" of parent with my "nom_ele_parent_id".
I try this:

SELECT a.nom_ele_id, a.nom_ele_title, b.nom_ele_title AS 'nom_parent' 
FROM `immo_nomenclature_element` AS a 
LEFT JOIN `immo_nomenclature_element` AS b 
    on a.`nom_ele_parent_id` = b.`nom_ele_id`

but with this request i can’t use WHERE nom_ele_title="type de sol" for exemple. and I get my entire table like this:

+-----------+-----------------+-------------------+
|nom_ele_id | nom_ele_title   | nom_parent        | 
+===========+=================+===================+
| 7060      | STANDARD        |   NULL            |  
| 6953      | SOL             |   STANDARD        | 
| 6957      | type de sol     |   SOL             |
+-----------+-----------------+-------------------+

2

Answers


  1. I am not sure if I get your question right.

    You only want a nom_parent if the nom_ele_title="type de sol" , right?

    Then you can just add a "and nom_ele_title="type de sol" to your join condition.

     SELECT    a.nom_ele_id,
              a.nom_ele_title,
              b.nom_ele_title             AS 'nom_parent'
    FROM      `immo_nomenclature_element` AS a
    LEFT JOIN `immo_nomenclature_element` AS b
    ON        a.`nom_ele_parent_id` = b.`nom_ele_id and a.nom_ele_title="type de sol"
    
    Login or Signup to reply.
  2. you just need to add where clause:

    SELECT a.nom_ele_id, a.nom_ele_title, b.nom_ele_title AS 'nom_parent' 
    FROM `immo_nomenclature_element` AS a 
    LEFT JOIN `immo_nomenclature_element` AS b 
        on a.`nom_ele_parent_id` = b.`nom_ele_id`
    where a.nom_ele_title='type de sol'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search