user3304748 user3304748 - 6 months ago 7x
SQL Question

Update third table by joining two table

I want to update third table "temp_table1" by performing some operations with other two tables "temp_table" "resource1".


| temp |
| 0.46574 |


| evaluation_value |
| 0.23 |
| 0.56 |
| 0.76 |
| 0.25 |
| 0.79 |


| temp |
| 0 |

Now, I want to subtract temp value from "temp_table" with all the evaluation_value from "resource1" and save the subtracted values which are less than 0.25 in "temp_table1" and I want to update the "temp_table1" every time I run the query.

I constructed the following query for this, but I am going wrong somewhere:

update temp_table1 t1
set t1.temp = (
select (e.evaluation_value - t.temp) < 0.25
from resource1 e
Inner join temp_table t)

Thank you!!!


Each time you run the query, you should clear existing values, then populate new ones:

TRUNCATE TABLE temp_table1

INSERT INTO temp_table1
    e.evaluation_value - t.temp
FROM temp_table t
    CROSS JOIN resource1 e
WHERE (e.evaluation_value - t.temp) < 0.25

That's because UPDATE only changes data in currently existing rows, and that is not your intention - you wish to get new results every time. Also, number of rows might vary each run, so UPDATE is not a good candidate here.

Another problem is with the JOIN type you picked: INNER is used in situations where there is a strict mapping from records in one table to the other (like an ID which exists in both tables) and it requires you to specify that mapping with ON keyword, while CROSS just joins everything in first table with everything in the other, like you wish to do (and so, no mapping is required).