使用Joins组合两个查询


Club two queries using Joins

假设我有两个查询,如下所示

select a.item1,b.item2 from tablename a,tablename b;
store a.item1 as $value1;
select c.item3 from tablename c where c.item4=$value1;

我需要将这两个查询合并为一个查询,并使用Joins

P.S: in PHP.

感谢您的帮助。

如果在三个表中有相同的字段,可以通过以下查询

select a.item1 as value1,b.item2,c.item3 from tablename a inner join tablename b on a.id=b.table1_id inner join tablename c on c.item4=value1;

问候,
Rekha

如果表名是"a"、"b"answers"c",则可以通过以下方式加入它们:

SELECT a.item1, b.item2, c.item3 FROM a, b
  INNER JOIN c ON a.item1 = c.item4;

不需要PHP代码,只需要SQL。

试试这个。。

select a.item1,b.item2,c.item3 from a join b on a.id = b.table_a.id
join c on a.item1 = c.item4