PHP比较两个表的结果

时间:2012-02-04 12:00:40

标签: php mysql compare

我有两个具有这种结构的表:

表一: ID说明

表二: ID名称

我必须只回显两个表的id,但我不知道如何比较。 我可以用一个愚蠢的例子来表达我的想法:

if($id is in the first table  AND $id is in the second table){
echo $id;
echo $description;
echo $name
}

怎么做? 谢谢!

3 个答案:

答案 0 :(得分:2)

尝试以下

select one.id from myTableOne one, myTableTwo two WHERE one.id=two.id

这将给出两个表中存在的ID列表......

祝你好运!!!

答案 1 :(得分:2)

$sql = mysql_query("SELECT t1.*,t2.* FROM table1 as t1, table2 as t2 WHERE t1.id=t2.id AND t1.id='$id'")
while($data = mysql_fetch_array($sql)){
   echo $data["id"];
   echo $data["description"];
   echo $data["name"];
}

答案 2 :(得分:2)

使用此查询,

SELECT 
    T1.ID, 
    T1.Description, 
    T2.Name 
FROM
    `TableOne` AS `T1`
INNER JOIN 
    `TableTwo` AS `T2`
ON 
    (T1.ID=T2.ID)