whitecat
whitecat

Reputation: 13

How to join 2 tables that already joined to others tables themsleves?

I currently have a query like this (I simplified to the maximum) :

SELECT d.id_doc, v.column1, t.label
FROM version v INNER JOIN person p USING(id_person),
     document d INNER JOIN type t USING(id_type)
WHERE d.id_doc = v.id_doc
AND ... ;

I'm wondering how can I replace the WHERE statement by a "real" JOIN.

Upvotes: 1

Views: 44

Answers (2)

Vulcronos
Vulcronos

Reputation: 3456

It should simply be:

SELECT d.id_doc, v.column1, t.label
FROM version v INNER JOIN person p USING(id_person)
INNER JOIN document d USING(id_doc)
INNER JOIN type t USING(id_type)

if this doesn't work let us know what sql platform you are using, SQL Server, Oracle, MYSQL?

In almost all platforms you can use:

INNER JOIN document d on v.id_doc = d.id_doc

as well.

Upvotes: 0

mucio
mucio

Reputation: 7119

Try in this way:

      SELECT d.id_doc, 
               v.column1, 
               t.label
          FROM version v 
    INNER JOIN person p 
         USING (id_person)
    INNER JOIN document d 
            ON d.id_doc = v.id_doc
-- or you can use also
--       USING (id_doc)
-- if it's present only in d and v tables
    INNER JOIN type t 
         USING (id_type)
         WHERE ... ;

Upvotes: 1

Related Questions