2011-11-27 33 views

回答

12

您可能想要考虑创建一个view

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

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


select * 
from MyView 
+0

之后呢?而已?我不打算更新它?或者其他的东西?因为我正在开发一个JDBC项目,并且我不想每次查找一些数据都继续使用join子句 – user962206

+0

我可以加入/查看两个以上的表吗?因为我有一张雇员表,个人信息和联系信息 – user962206

+0

@ user962206是和是。您可以根据需要加入任意数量的表格。 – Bert

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;