简体   繁体   中英

Right Outer Join to Left Outer join

I have a query in sqlserver that needs to be translated to sqlite3, but this query uses "Right outer join", the problem is that sqlite doesn't support this operator yet.

how can I translate this query to use only "left outer join" instead of "right" outer join?

SELECT  *
FROM    P RIGHT OUTER JOIN
        CL RIGHT OUTER JOIN
        C LEFT OUTER JOIN
        CC ON C.IDC = CC.RIDC ON 
        C.IDC = C.RIDCL ON P.IDP = C.RIDP

Thanks.

PS: I'm having trouble also with the sqlite joins precedence and associativity, don't know how it may alter the final result by reordering the tables.

In this query, the table c is in the middle. So that is driving the query.

Your on conditions are in strange places. I am guessing this is what you mean:

SELECT  *
FROM    C LEFT OUTER JOIN
        CC
        ON C.IDC = CC.RIDC LEFT OUTER JOIN
        P
        ON P.IDP = C.RIDP LEFT OUTER JOIN
        CL
        ON CL.IDC = C.RIDCL

An additional remark on the join syntax.

Most people write

Tab1 JOIN Tab2 ON ... JOIN Tab3 ON ... JOIN Tab4 ON ...

probably because this is the "natural" way, one table after the other.

But your joins use the "other" syntax i usually try to avoid.

Tab1 JOIN Tab2 JOIN Tab3 JOIN Tab4 ON ... ON ... ON ... 

Logically joins are processed in the order of ON, so adding parens results in:

( P
  RIGHT OUTER JOIN
   ( CL
     RIGHT OUTER JOIN
      ( C
        LEFT OUTER JOIN
        CC
        ON C.IDC = CC.RIDC
      ) p
     ON C.IDC = C.RIDCL
   ) 
  ON P.IDP = C.RIDP
)

Rewriting it results in a join order C-CC-CL-P. This is not the C-CC-P-CL order Gordon used, but in your case this doesn't matter, because C is the main table :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM