Matteo Mosconi
Matteo Mosconi

Reputation: 244

PHP compare result of two tables

I have two tables with this structure:

Table one: ID Description

Table two: ID Name

I have to echo only the id's of both tables but i don't know how to compare. i can show my idea using a stupid example:

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

How to do this? Thanks!

Upvotes: 0

Views: 2107

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

use this query,

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

Upvotes: 2

mgraph
mgraph

Reputation: 15338

$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"];
}

Upvotes: 2

Fahim Parkar
Fahim Parkar

Reputation: 31627

try below

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

This will give the list of ids that are present in both tables...

Good Luck!!!

Upvotes: 2

Related Questions