| title | Update a Table |
|---|---|
| sidebarTitle | Update a Table |
MindsDB provides two ways of using the UPDATE statement:
-
The regular
UPDATEstatement updates specific column values in an existing table. -
The
UPDATE FROM SELECTstatement updates data in an existing table from a subselect query. It can be used as an alternative toCREATE TABLEorINSERT INTOto store predictions.
Here is an example of the regular UPDATE statement:
UPDATE integration_name.table_name
SET column_name = new_value
WHERE column_name = old_valueintegration_nameis the name of the connected data source.table_nameis the table name within that data source.column_nameis the column name within that table.
And here is an example of the UPDATE FROM SELECT statement that updates a table with predictions made within MindsDB:
UPDATE
integration_to_be_updated.table_to_be_updated
SET
column_to_be_updated = prediction_data.predicted_value_column,
FROM
(
SELECT p.predicted_value_column, p.column1, p.column2
FROM integration_name.table_name as t
JOIN model_name as p
) AS prediction_data
WHERE
column1 = prediction_data.column1
AND column2 = prediction_data.column2Below is an alternative for the UPDATE FROM SELECT statement that updates a table with predictions. This syntax is easier to write.
UPDATE
integration_to_be_updated.table_to_be_updated
ON
column1, column2
FROM
(
SELECT p.predicted_value_column as column_to_be_updated, p.column1, p.column2
FROM integration_name.table_name as t
JOIN model_name as p
) - It executes query from the
FROMclause to get the output data. In our example, we query for predictions, but it could be a simple select from another table. Please note that it is aliased asprediction_data. - It updates all rows from the
table_to_be_updatedtable (that belongs to theintegration_to_be_updatedintegration) that match theWHEREclause criteria. The rows are updated with values as defined in theSETclause.