SQL query in MySQL between 3 tables

3

I have in MySql 3 tables, of which 1 contains the code of the other two tables I try to make a select that shows me the first table and instead of the ID of the other 2 tables show me the corresponding name

tabla_1
int id_tabla1
varchar nombre_tabla1
int id_tabla2
int id_tabla3

tabla_2
int id_tabla2
varchar nombre_tabla2

tabla_3
int id_tabla3
varchar nombre_tabla3

The select I've tried is this

Select * from tabla_1,tabla_2,tabla_3 where tabla_1.id_tabla2 = tabla_2.id_tabla2 and tabla_1.id_tabla3 =  tabla3.id_tabla3 
    
asked by Hersenfels 29.07.2018 в 02:52
source

1 answer

4

It is clearer to use JOIN to join the tables, for example:

SELECT 
    t2.nombre_tabla2,
    t3.nombre_tabla3
FROM tabla_1 t1
    INNER JOIN tabla_2 t2 ON t1.id_tabla2=t2.id_tabla2
    INNER JOIN tabla_3 t3 ON t1.id_tabla3=t3.id_tabla3

Here, t1, t2, t3 are aliases to simplify then the JOIN and the columns of SELECT .

In the SELECT you can add the columns you want from each table. It is always convenient to use the alias of each table for each column, in this way confusion is avoided, especially when there are columns that are called equal and are in different tables.

    
answered by 29.07.2018 / 03:01
source