There are many scenarios where it is required to update a table either directly or using another table.
➠ Sample Table and Data
Table 1
CREATE ROWSTORE TEMPORARY TABLE updatetable(
id INTEGER,
name VARCHAR(100),
indicator_flag CHAR(1),
SHARD KEY(id)
);
Table 2
CREATE ROWSTORE TEMPORARY TABLE othertable(
id INTEGER,
name VARCHAR(100),
indicator_flag CHAR(1),
SHARD KEY(id)
);
Table 1:
INSERT INTO updatetable VALUES(1,'SingleStore',null);
INSERT INTO updatetable VALUES(2,'Database',null);
INSERT INTO updatetable VALUES(3,'Oracle',null);
INSERT INTO updatetable VALUES(4,'Vertica',null);
Table 2:
INSERT INTO othertable VALUES(1,'SingleStore','Y');
INSERT INTO othertable VALUES(2,'Database','N');
INSERT INTO othertable VALUES(5,'DB2','Y');
INSERT INTO othertable VALUES(6,'MYSQL','Y');
UPDATE updatetable
SET indicator_flag='Y'
WHERE id=4;
Output:
Rows matched: 1 Changed: 1 Warnings: 0.
UPDATE updatetable
SET indicator_flag='Y',
name='Hive'
WHERE id=4;
Output:
Rows matched: 1 Changed: 1 Warnings: 0
UPDATE updatetable , othertable
SET indicator_flag = othertable.indicator_flag
WHERE updatetable.id = othertable.id;
Output:
Rows matched: 2 Changed: 2 Warnings: 0
UPDATE updatetable , othertable AS tempTableAlias
SET indicator_flag = tempTableAlias.indicator_flag
WHERE updatetable.id = tempTableAlias.id;
Output:
Rows matched: 2 Changed: 2 Warnings: 0