我有两个数据库,这些数据库在不久前开始。这些现在需要合并为一个。为避免重复,我想删除第二个条目。如果我运行下面的查询,它会自动删除重复项: -
SELECT idClient, LastName, FirstName
FROM `ABC`.`Client`
WHERE idClient > 1000
UNION
SELECT idClient, LastName, FirstName
FROM `XYZ`.`Client`
WHERE idClient > 1000;
但是,我想要返回的唯一条目是重复的条目,但无法解决语法问题。
答案 0 :(得分:1)
使用INNER JOIN
SELECT idClient, LastName, FirstName
FROM `ABC`.`Client` a
JOIN `XYZ`.`Client` b ON a.idClient = b.idClient AND a.LastName = b.LastName AND a.FirstName = b.FirstName
WHERE a.idClient > 1000
答案 1 :(得分:1)
如果您想要重复的那些,可以使用exists
:
SELECT idClient, LastName, FirstName
FROM `ABC`.`Client` ac
WHERE idClient > 1000 AND
EXISTS (SELECT 1
FROM `XYZ`.`Client` xc
WHERE xc.idClient = ac.idClient AND xc.LastName = ac.LastName AND
xc.FirstName = ac.FirstName AND
xc.idClient > 1000
);
注意:这不会返回具有NULL
值的列。