2011-11-27 30 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;