| title | UPDATE FROM SELECT Statement |
|---|---|
| sidebarTitle | UPDATE |
The UPDATE FROM SELECT statement updates data in existing table. The data comes from a subselect query.
It can be used as alternative to 'create table' and 'insert into' for store predictions in distinct columns of existing rows
Here is an example:
UPDATE
int2.table2
SET
predicted = source.result,
FROM
(
SELECT p.result, p.prod_id, p.shop_id
FROM int1.table1 as t
JOIN mindsdb.pred1 as p
) AS source
WHERE
prod_id = source.prod_id
and shop_id = source.shop_idAnd the steps followed by the syntax:
- It executes query from 'FROM' block to get the output data. In our example it is join of table table1 (from integration int1) with predictor pred1. It also can be select from integration
- source is the alias for fetched data
- then it updates table2 from int2 using conditions from
WHEREblock and fields for update fromSETblock- under the hood it splits input data to rows and execute this query for every row:
UPDATE table2 SET predicted = <row.result>, WHERE prod_id = <row.prod_id> and shop_id = <row.shop_id>