如何使用SQL永久连接两个表?

时间:2011-11-27 07:23:29

标签: mysql sql join view

是否可以永久加入两个表?我只是在执行该查询后执行,之后即使退出DBMS,它也可以自动加入?

2 个答案:

答案 0 :(得分:13)

您可能需要考虑创建view

视图本质上是一个存储的SQL语句,您可以像查询表一样进行查询。

create view MyView as
    select TableA.Field1, TableB.Field2
    from TableA
    join TableB on TableB.ID = TableA.ID


select *
from MyView

答案 1 :(得分:0)

如果您总是同时写入并从联接中读取,则可以将它们合并为一个并从中进行编写和选择。

--==[ before ]==--
insert into user (id, name) values (1, "Andreas");
insert into email (id, email) values (1, "andreas - at - wederbrand.se");

select user.id, user.name, user.email from user, email where user.id = email.id;

--==[ do the merge ]==--
create table user_with_email select user.id, user.name, user.email from user, email where user.id = email.id;

drop table user;
drop table email;

--==[ after ]==--
insert into user_with_email id, name, email values (2, "Bruce", "Bruce - at - springsteen.com");

select id, name, email from user_with_email;