MySQL 中是否有一种方法可以使用视图未涵盖的 table 中的数据更新视图?
Is there a way in MySQL to update a view with data from a table that is not covered by the view?
我有两个表,test1 和 test2:
test1
| id | name |
| -- | ----- |
| 1 | Steve |
| 2 | Phil |
| 3 | Tony |
test2
| id | name |
| -- | ----- |
| 4 | Peter |
| 5 | Perry |
我使用代码
从 test1 创建了一个视图
CREATE VIEW test_view AS SELECT * from test1;
我想用 test2 的数据更新此 test_view。
我想要的是如下图
| id | name |
| -- | ----- |
| 1 | Steve |
| 2 | Phil |
| 3 | Tony |
| 4 | Peter |
| 5 | Perry |
我希望找到一种无需连接两个表即可执行此操作的方法。
您可以使用联合。
create table a (id int, name varchar(255));
create table b (id int, name varchar(255));
insert into a values (1, "c"), (2, "d");
insert into a values (5, "zz"), (6, "ff");
insert into b values (1, "a"), (2, "b");
create view c as select * from a union select * from b;
select * from c;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
| 1 | c |
| 2 | d |
| 5 | zz |
| 6 | ff |
+------+------+
根据 Solarflare -
A view is just the result of a query. The "content" of a view changes
when you change the underlying data (e.g. table test1) - and it
doesn't change if you don't change the underlying data
因此,当我执行 test1 = test1.append(test2)
时,我从底层 test1 数据帧创建的 test_view 会自动更新。
我有两个表,test1 和 test2:
test1
| id | name |
| -- | ----- |
| 1 | Steve |
| 2 | Phil |
| 3 | Tony |
test2
| id | name |
| -- | ----- |
| 4 | Peter |
| 5 | Perry |
我使用代码
从 test1 创建了一个视图
CREATE VIEW test_view AS SELECT * from test1;
我想用 test2 的数据更新此 test_view。
我想要的是如下图
| id | name |
| -- | ----- |
| 1 | Steve |
| 2 | Phil |
| 3 | Tony |
| 4 | Peter |
| 5 | Perry |
我希望找到一种无需连接两个表即可执行此操作的方法。
您可以使用联合。
create table a (id int, name varchar(255));
create table b (id int, name varchar(255));
insert into a values (1, "c"), (2, "d");
insert into a values (5, "zz"), (6, "ff");
insert into b values (1, "a"), (2, "b");
create view c as select * from a union select * from b;
select * from c;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
| 1 | c |
| 2 | d |
| 5 | zz |
| 6 | ff |
+------+------+
根据 Solarflare -
A view is just the result of a query. The "content" of a view changes when you change the underlying data (e.g. table test1) - and it doesn't change if you don't change the underlying data
因此,当我执行 test1 = test1.append(test2)
时,我从底层 test1 数据帧创建的 test_view 会自动更新。