Use AND within an INNER JOIN can?

5

I wonder if it is possible to use AND within a INNER JOIN , eg:

SELECT *
  FROM tab1
 INNER JOIN tab2
    ON tab2.id2 = tab1.id1
   AND tab2.camp1 = 'valor'
 INNER JOIN tab3
    ON tab3.id3 = tab2.id2;

I gave a rather silly example, but the question is if you have some way to use AND after ON and before another INNER JOIN ?

    
asked by anonymous 10.02.2017 / 14:35

2 answers

5

Yes, it is possible to have one or more JOIN class consisting of operators such as AND , OR <> etc. The equi join is more common anyway.

Basically the compound join already discards records that do not meet the criteria in the join. If you have a 'simple' join and later a WHERE means that the discard will be in the constraint, ie the first one is the join and then the discard.

The 'discard' done in JOIN (junction)

SELECT * FROM t1
INNER JOIN t2 ON t1.id = t2.id AND td2.data = '2017-02-10'

The 'discard' made in WHERE (restriction)

SELECT * FROM t1 
INNER JOIN t2 ON t1.id = t2.id
WHERE td2.data = '2017-02-10'
    
10.02.2017 / 14:41
6

Yes you can, but it is the case to use it only when the key is composed. Restriction of results should be done in WHERE .

    
10.02.2017 / 14:38