简体   繁体   中英

PostgreSQL - Update with JOIN - inner or outer?

I come from SQL Server and I am migrating some T-SQL code to Postgres.

In PostgreSQL, I now have this UPDATE statement (see below).

In there:
"#reportdata" is a temporary table
kwt.Report is a normal table

This part in the WHERE clause is doing an implicit JOIN. I think that's how they call it in Postgres.

(cr.campaignid = rp.campaignid AND cr.reportdate = rp.reportdate)

That is because this couple (campaignid, reportdate) represents a unique logical key in kwt.Report. Also, both columns are not nullable in kwt.Report. In "#reportdata" both columns can be NULL.

My question is: when I see such an implicit join in an UPDATE statement, I am somehow always not quite sure if it's INNER or OUTER join. I think it's INNER, there's no way this to be OUTER but I just want to be sure.

Could someone please confirm?

I mean, OK, if rp.campaignid is NULL there's no way this condition to evaluate to true, right?

(cr.campaignid = rp.campaignid AND cr.reportdate = rp.reportdate)

I am asking this, because I am not sure if comparison with NULL works the same way in Postgres as in SQL Server. As far as I recall, in SQL Server NULL = a always evaluates to NULL (not to true (bit 0), not to false (bit 1) but to NULL). Please correct me if this understanding is not right. Is this the same in Postgres?

UPDATE kwt.Report cr 
SET 
    impressions = rp.impressions,
    clicks = rp.clicks,
    views = rp.views 
FROM 
    "#reportdata" AS rp
WHERE
    (cr.campaignid = rp.campaignid AND cr.reportdate = rp.reportdate)
    AND (rp.campaignid IS NOT NULL);

In SQL:

A = null is neither true nor false

Check this

 with cte0 as ( select '1' as c ), cte1 as ( select null as c ) select * from cte0 inner join cte1 on cte0.c = cte1.c union select * from cte0 inner join cte1 on cte0.c != cte1.c 
\nc |  c  \n:- |  :- \n

db<>fiddle here

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