我有 6 个具有相同表名的 mysql 数据库,例如带有字段 email_address 的 users_table,现在我想从所有 6 个数据库中获取任何一个数据库表中缺少的 email_address。
请给我一些相同的建议。
我有 6 个具有相同表名的 mysql 数据库,例如带有字段 email_address 的 users_table,现在我想从所有 6 个数据库中获取任何一个数据库表中缺少的 email_address。
请给我一些相同的建议。
这一个选择在其中一个数据库中丢失的所有电子邮件,并指出它们存在于哪个数据库中。
SELECT email, GROUP_CONCAT(db_Name) AS emailIsOnlyIn FROM (
SELECT 'db1' as db_Name, email FROM db1.yourTable
UNION ALL
SELECT 'db2' as db_Name, email FROM db2.yourTable
UNION ALL
SELECT 'db3' as db_Name, email FROM db3.yourTable
UNION ALL
SELECT 'db4' as db_Name, email FROM db4.yourTable
UNION ALL
SELECT 'db5' as db_Name, email FROM db5.yourTable
UNION ALL
SELECT 'db6' as db_Name, email FROM db6.yourTable
) subquery_alias
GROUP BY email
HAVING COUNT(DISTINCT db_name) < 6;
如果您想知道电子邮件丢失在哪个数据库中,则要复杂一些:
SELECT
*
FROM (
SELECT email FROM db1.yourTable
UNION
SELECT email FROM db2.yourTable
UNION
SELECT email FROM db3.yourTable
UNION
SELECT email FROM db4.yourTable
UNION
SELECT email FROM db5.yourTable
UNION
SELECT email FROM db6.yourTable
) sq,
( SELECT 'db1' as db_Name2
UNION ALL
SELECT 'db2' as db_Name2
UNION ALL
SELECT 'db3' as db_Name2
UNION ALL
SELECT 'db4' as db_Name2
UNION ALL
SELECT 'db5' as db_Name2
UNION ALL
SELECT 'db6' as db_Name2
)dummy
WHERE NOT EXISTS (SELECT 1 FROM (
SELECT 'db1' as db_Name, email FROM db1.yourTable
UNION ALL
SELECT 'db2' as db_Name, email FROM db2.yourTable
UNION ALL
SELECT 'db3' as db_Name, email FROM db3.yourTable
UNION ALL
SELECT 'db4' as db_Name, email FROM db4.yourTable
UNION ALL
SELECT 'db5' as db_Name, email FROM db5.yourTable
UNION ALL
SELECT 'db6' as db_Name, email FROM db6.yourTable
) subquery_alias
WHERE sq.email = subquery_alias.email AND subquery_alias.db_Name = dummy.db_Name2
)