I have 2 table in my database.
Table #1
----------------------------
| name | id | overdue_days |
----------------------------
Table #2
-------------
| name | id |
-------------
This is to maintain a fine collection database where overdue_days
in table #1 corresponds to how many days the fine submission by a particular id pending is done.
Table #2 receives every day the names of defaulters for that day.
My intention is to compare table #1 and table #2 and
- Increment the overdue days for ids which are present in table #2 and are already present in table #1.
- If a particular id is present in table #1 but not in table #2 , then set its overdue days to 0.
- If an id is present in table #2 but not in table #1, insert a row for it with overdue days as value 1.
My desired result would be updated table #1 with the above conditions.
I can think of one way to use three separate join queries and cover each of the cases.
update T1
set overdue_days = overdue_days + 1
from table#1 T1
inner join table#2 T2
on T1.id = T2.id;
Is there an optimised way to do the above?
Best Answer
For MySQL only.
If the
id
field in thetable1
is primary key or has unique constraint, the solution can consist of 2 queries:overdue_days
is set to 0 by Q2.overdue_days
is incremented by Q2.overdue_days
= 0, and then is incremented by Q2.For PostgreSQL.
overdue_days
is set to 0 by Q2.overdue_days
is incremented by Q1 and is not affected by Q2.overdue_days
= 1, which is not affected by Q2.