简体   繁体   中英

MySQL Join 3 Tables Reflexive Relation

I am using the following script:

SELECT autor.id_autor as 'ID de Autor', 
autor.nom_autor as 'Nombre Autor',
CASE autor.id_autor
WHEN  ISNULL(autor.pseudo_id = autor.id_autor ) THEN  nom_autor
ELSE  null
END 
'Autor Real', -- nombre de la columna de la tabla

articulo.id_art as 'ID de Articulo',
articulo.nom_art as 'Nombre de Articulo'
FROM autor
LEFT JOIN art_aut ON (art_aut.id_autor = autor.id_autor)
LEFT JOIN articulo ON(articulo.id_art = art_aut.id_art);

to show the real name of aun author.

I have many tables as you can see but I want those tables to "feed" the following table in order to appear just like the following:

  ID de Autor   Nombre Autor   Autor Real  ID de Articulo   Nombre Articulo
     A3            Quijote       Luis           1             El origen

I have a table called autor which is the following:

id_autor   nom_autor    RFC      pseudo_id
 A1         J Salinas   ADAD12    
 A3         Quijote                 A7
 A7         Luis        LIDSDS      

As you can see the table references to itself but I do not know what kind of instructions I have to write in order that the authors just as "Quijote" shows their real name and the ones that do not have another name just as Juanito Salinas, stay the same because they do not have a record in the pseudo_id column

Thanks in advance!

I'm not fully understanding your query.

If I'm understanding correctly, your autor table as a column named pseudo_id , which is a foreign key that references the id_autor column in the same table.

To retrieve the related row, you could perform a JOIN operation, likely you want an "outer" join so that all rows from the autor tale are returned, along with the "matching" row if one is available.

For example:

SELECT a.id_autor
     , a.nom_autor
     , r.nom_autor AS `Autor Real`
     , ...

  FROM autor a
  LEFT
  JOIN autor r ON r.id_autor = a.pseudo_id

The Autor Real value will have the value from the nom_autor column from the referenced row ( r ) when a matching row is found, and will otherwise it will be NULL.

The "trick" here is that you need to have a second reference to the autor table in the query to retrieve that second (related) row. It's as if that second reference to the autor table is to a different table, just like any additional tables referenced by your query.


As another (usually less performant) alternative, you could use a correlated subquery in place of the JOIN operation.

For example:

SELECT a.id_autor
     , a.nom_autor
     , ( SELECT r.nom_autor 
           FROM autor r
          WHERE r.id_autor = a.pseudo_id
          ORDER BY r.nom_autor
          LIMIT 1
       ) AS `Autor Real`
     , ...
  FROM autor a

That's a "correlated" subquery in the SELECT list. That query references a column from the outer query, and that subquery gets executed for each row returned by the outer query.) If the subquery doesn't return a value, the outer query returns a NULL value.

(NOTE: The ORDER BY and LIMIT clauses may not be needed in your query. Especially if id_autor column is UNIQUE in the autor table. They're included here to demonstrate the more general case, where there may be more than one "matching" row; the subquery in the SELECT list is allowed to return only a single expression, and a single row. The LIMIT 1 clause limits it to a single row, and the ORDER BY makes deterministic which of the possible values would be returned.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM