开发者

How to use a CTE in a left outer join?

开发者 https://www.devze.com 2023-04-11 16:23 出处:网络
I am trying to join a common table expression to an existing table (table1) as follows. select column1, column2 from table1

I am trying to join a common table expression to an existing table (table1) as follows.

select column1, column2 from table1

left outer join

  ;with cte as (
    select column1, column2 from table2)

  select column1, column2 from cte

on table1.column1 = cte.colum开发者_如何学运维n1

The errors are:

  1. Incorrect syntax near ';'.
  2. Incorrect syntax near the keyword 'on'.

What am I doing wrong? Should I be using a CTE for this task?


The CTE must come at the beginning of the query.

with cte as (
    select column1, column2 from table2
)
select column1, column2 from table1
   LEFT JOIN cte
on table1.column1 = cte.column1;
0

精彩评论

暂无评论...
验证码 换一张
取 消