PostgreSQL Finding IDs that don't exist in two other tables -


i want change io_dl field 1 0 in io table, if 3 conditions hold true

io table pkey -> io_id

  1. the pkey io not in entire table m fkey named m_id
  2. the pkey io not contained in table p, column p_id csv delimited string of ids ex. "1923,2309,210"
  3. the current value io_dl field in io table set 1

step 3 guess isn't needed setting 0 0 doesn't mess up, check may slow query down?

this i've tried , im getting massive list of same io_ids listed, think i'm using join or union wrongly.

update io set io_dl = 0 io_id in (     select i.io_id io         inner join (             select p_id "io_id" p             union             select regexp_split_to_table(m.m_id, ',')::integer             id m             ) q         on i.io_id != q.io_id     i.io_dl = 1 ); 

made own solution few minutes later simpler query believe.

update io set io_dl = 0 io_id in (     select i.io_id io     i.io_id not in (         select p_id "io_id" p         union         select regexp_split_to_table(m.m_id, ',')::integer "io_id" m     ) , i.io_dl = 1 ); 

use except distinct ids:

update io set io_dl = 0 io_id in (     select io_id io     except (         select p_id p         union         select regexp_split_to_table(m_id, ',')::integer m     ) ) , io_dl = 1 returning *; 

by adding returning * can control how many rows updated.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)