我有一个问题,我觉得很简单,但我似乎遇到了一些问题。
我有两张桌子。每个表都有完全相同的行。
我尝试使用以下查询在两个表上执行连接:
SELECT t1.`* FROM `person` as t1
JOIN `person_temp` as t2
on t1.`date` = t2.`date`
and t1.`name` = t2.`name`
and t1.`country_id`= t2.`country_id`
此查询的目的是查找t1中与t2匹配的所有行,其中date,name和country_id的组合相同(这三列组合使记录唯一)。我不认为这个查询对我正在尝试做的是正确的,因为如果我在两个表中都有相同的确切数据,那么我会得到更多的匹配。
有关如何编辑此查询以完成我要执行的操作的任何想法?
答案 0 :(得分:1)
请勿使用join
。使用exists
:
SELECT t1.`*
FROM `person` t1
where exists (select 1
from `person_temp` as t2
where t1.`date` = t2.`date`
and t1.`name` = t2.`name`
and t1.`country_id`= t2.`country_id`
);
对于性能,您需要person_temp(date, name, country_id)
上的复合索引(列可以按任何顺序排列)。