gpt4 book ai didi

php - 比较两个表的最快方法

转载 作者:行者123 更新时间:2023-11-29 01:37:21 25 4
gpt4 key购买 nike

我有两个这样结构的表

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

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

我必须比较这两个表并找出哪些没有改变(在这种情况下我应该只找到 tid 1)

我可以创建 2 个数组用于循环,但因为每个表都有超过 40k 条记录。我正在寻找更简单、更快速的方法!

编辑。看起来我应该使用连接。我试图做的如下。附言。表1名称为2016-5-20 表2名称为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);

最佳答案

你可以加入他们:

SELECT t1.tid, t1.population
FROM table1 t1
JOIN table2 t2 ON t1.tid = t2.tid AND t1.population = t2.population

关于php - 比较两个表的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37366236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com