Mysql join syntax

If I want to do joins on 3 or more tables, what's the best syntax? This is my attempt:

Select * 
from table1 
inner join table2 using id1, table2 
inner join table3 using id2, table3 
inner join table4 using id4 
where table2.column1="something" 
and table3.column4="something_else";

does it look right? What I'm not sure about is 1) I need to separate the compounds with a comma 2) Can I make all my entry fees first and then set my conditions after that? 3) it would be better to use subqueries, and if so, what is the corect syntax

Thanks for any advice!

+3
source share
6 answers
  • Avoid use whenever possible *.

    Indicate exactly the data you want to return.

  • .

    , , .

    , .

  • , WHERE INNER JOIN, ; WHERE.

  • , . , . USING , .

  • MySQL , . , , .

!

select t1.*
       , t2.*
       , t3.*
       , t4.*

from   table1 t1

       inner join table2 t2
       on t1.id = t2.t1_id
    and
       t2.column1 = "something"

       inner join table3 t3
       on t2.id = t3.t2_id
    and
       t3.column4 = "something_else"

       inner join table4 t4
       on t3.id = t4.t3_id;
+3

1)

2) , ?

3) , , corect

. .

+1
  • ANSI ,

. SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE table2.column1 = 'Something'

  1. 100%, . , .

, , , .

, . , .

+1

, table2 id1 - table1 table2, table3 id2 - table2 table3 table4 id3 - table3 table4, :

SELECT * 
FROM table1 
INNER JOIN table2 ON table2.id1 = table1.id1
INNER JOIN table3 ON table3.id2 = table2.id2
INNER JOIN table4 ON table4.id3 = table3.id3
WHERE table2.column1 = "something" 
    AND table3.column4 = "something_else"

, , - USING -.

+1

, :

Select * 
from table1 
inner join table2 using id1
inner join table3 using id2
inner join table4 using id4 
where table2.column1="something" 
and table3.column4="something_else"

id4 1, , :

inner join table4 on table4.id = table1.table4i
0

You might be able to use a natural join that joins the field names common to the tables you want to join as follows.

SELECT * FROM table1 NATURAL JOIN table2 NATURAL JOIN table3 NATURAL table4 WHERE table2.column1 = "something" AND table3.column4 = "something_else"

0
source

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


All Articles