开发者

Double tables select show?

I have a problem...I have 2 tables, Table1 and Table2.

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

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

Table2 has many id and agent ids in it. The data for the id in table 2 came from table 1.

I have an agentid say $aid. This has many id's associated with it in table 2.

I am trying to get the set of text values out from table 1 associated with all ids which are related 开发者_StackOverflow中文版to agentid $aid from table 2.

Does that make sense?!

Any ideas?


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

This will give you all text rows for given agentid. The same can be done using join too -

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


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


The query:

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

Try not to include the $aid directly in the query, but escape it and/or use prepared statements with query parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜