2011-03-25 27 views
0

我有一個問題...我有2個表格,Table1和Table2。雙桌選擇秀?

Table1: 
id,int(11) 
text,varchar(11) 

Table2: 
id,int(11) 
agentid,int(11) 
unique(id,agentid) 

表2中有很多id和agent id。表2中id的數據來自表1.

我有一個agentid說$援助。這與它在表相關的許多ID的2

我試圖讓一組文本從被以AGENTID從表2

這是否$援助有關的所有ID相關聯的表1值了合理?!

任何想法?

回答

0
select text from table1 where id in 
(
select id from table2 where agentid = <your aid> 
) 

這會給你給定agentid的所有文本行。 同樣可以使用加入來完成 -

select t1.text from table1 t1 
inner join table2 t2 
on t1.id = t2.id 
where t2.agentid = <your aid> 
+0

其中一種方法特別喜歡另一種嗎?就像是小桌子等最快或最好的一個? – David19801 2011-03-25 12:14:49

+0

@ David19801 - 我認爲這個連接比另一個更好,這取決於你在表中有多少條記錄。更多的記錄,加入表現更好。 – 2011-03-25 12:16:38

0
select * from table1 as t1 inner join table2 as t2 
on t1.id = t2.id 
where agentid = $aid 
0

查詢:

$sql = "select t1.text from table1 t1, table2 t2 where t2.id = t1.id and t2.agentid = {$aid}"; 

儘量不包括直接援助$查詢中,但逃避它和/或使用與查詢參數預處理語句。