I'm curious to know if this is actually possible...
In this simplified example, I have:
Table_A: Table_B:
+------+--------+ +------+--------+
| id | data_1 | | id | data_2 |
+------+--------+ +------+--------+
| 1 | Hello | | 1 | There |
+------+--------+ +------+--------+
| 2 | Hi |
+------+--------+
A MySQL View (CREATE VIEW...
) called tables
is:
SELECT table_a.id AS id,
table_a.data_1 AS data_1,
table_b.data_2 AS data_2
FROM table_a
LEFT JOIN table_b
ON table_a.id=table_b.id
So, a simple SELECT * FROM tables
will return:
+------+--------+--------+
| id | data_1 | data_2 |
+------+--------+--------+
| 1 | Hello | There |
+------+--------+--------+
| 2 | Hi | NULL |
+------+--------+--------+
An update to existing data is fine. Ie., UPDATE tables SET data_2='World' WHERE id=1
completes and the result for id 1 would be:
+------+--------+--------+
| id | data_1 | data_2 |
+------+--------+--------+
| 1 | Hello | World |
+------+--------+--------+
But what about using an UPDATE
statement on the right table (table_b
) if it does not have any data matching the id? Using the same example as earlier, UPDATE tables SET data_2='John' WHERE id=2
will result in 0 Rows matched (and therefore, tables
will still return NULL
instead of John
in the data_2
field).
+------+--------+--------+
| id | data_1 | data_2 |
+------+--------+--------+
| 2 | Hi | NULL |
+------+--------+--------+
Is there a way to make this work on the tables
view, without having to touch the table_b
directly?