get name of an id from another table

0

I have this table

|ID|nombre |   
|1 |Ricardo|  
|2 |Roberto|
|3 |Brayan |   

And then it's another one

|ID_comment|Escrito_por|dirigido a|Comentario |
|1         |2          | 1        | Comentario| 
|2         |3          | 1        | Comentario|
|3         |2          | 1        | Comentario|
|4         |4          | 1        | Comentario|
|5         |3          | 1        |           |

My problem is that I do not know in what way I can show the name of the person who directs the comment that is fk of the users table with the other data. I tried with join or a subquery,

The result I'm looking for is:

| ID_comment | Written_by | directed to | Comment | name of who wrote |

but in the field of the person who 'written by' the value can be repeated. to which the query bounces the first 4 fields correctly but the name of the person who wrote bounces a repeated query

| ID_comment | Written_by | directed to | Comment | name of who wrote |

| 1 | 2 | 1 | Comment | Ricardo |

| 2 | 2 | 1 | Comment | Ricardo |

| 3 | 2 | 1 | Comment | Ricardo |

As you see the comment 2 repeats the name of Ricardo when he did not write it. Here is my select:

SELECT nombre , Id_comment , commentario , dirigido a FROM comments FULL JOIN users ON users . escrito por = 2

probe with all the joins ah add another table but it keeps repeating that value and I do not know how to fix it.

    
asked by Jesus Garcia Barca 11.02.2018 в 13:15
source

1 answer

0

When you do a JOIN you have to put the columns by which you join the tables in the On part, in this case you add comments with users by the ID of the writer and the corresponding id in the table of users

SELECT name, Id_comment, commentary, addressed to FROM comments FULL JOIN users ON comments.escrito por = users.id

    
answered by 12.02.2018 / 00:21
source