Mesut Aslan Mesut Aslan - 6 months ago 9
PHP Question

Fastest way for comparing two tables

I have two tables with a structure like this

Table 1
tid | population
1 | 50
2 | 55
3 | 45

Table 2
tid | population
1 | 50
2 | 60
3 | 40


I must compare these two tables and find out which ones hasn't changed (in this case i should only find tid 1)

I can create 2 arrays for looping but since each table has more than 40k records. I'm looking for easier, faster way!

Edit.
Looks like i should use joins. What I've tried to do is as follows.
Ps. Table 1 name is 2016-5-20 Table 2 name 2016-05-21

$sth = $conn->prepare('SELECT `2016-5-20.tid`, `2016-5-20.population`
FROM `2016-5-20`
JOIN `2016-05-21` ON 2016-5-20.tid = 2016-05-21.tid AND 2016-5-20.population = 2016-05-21.population');
$sth->execute();
$result2 = $sth->fetchAll();
print_r($result2);

Answer
 select `t1`.`id` from `table1` as `t1`,`table2` as `t2` 
 where `t1`.`id`=`t2`.`id` and `t1`.`population`<>`t2`.`population`

Fiddle

Comments