[英]MySQL UNION vs JOIN with OR in clause
I have one table TableA
with columns [AccountID, Email]
. 我有一个表
TableA
,其中有[AccountID, Email]
列。 I have another table TableB
with [AccountID_1, AccountID_2, email]
. 我还有另一个具有
[AccountID_1, AccountID_2, email]
表TableB
。 I need to match the AccountID
in TableA
with either of the AccountIDs
in TableB
. 我需要比赛
AccountID
在TableA
与任一的AccountIDs
在TableB
。 Neither of these approaches seem to be working. 这些方法似乎都不起作用。 The first one seems to be stalling or just taking forever (both tables have several hundred thousand entries).
第一个似乎停滞不前或只是永久使用(两个表都有几十万个条目)。 The second has the error
"Can't reopen table: TableB".
第二个错误为
"Can't reopen table: TableB".
select count(distinct TableA.id) from TableA
JOIN TableB ON TableB.AccountID = TableA.AccountID_1
OR TableB.AccountID = TableA.AccountID_2
;
select count(distinct b.id) from (
select * from TableA
join TableB on TableB.AccountID = TableA.AccountID_1
union
select * from TableA
join TableB on TableB.AccountID = TableA.AccountID_2
) as b;
You could try 你可以试试
select count(*) from TableA where
exists ( select 1 from TableB where
AccountID in (AccountID_1,AccountID_2))
For the first one, the column name being selected is wrong. 对于第一个,选择的列名是错误的。 The following seem to work fine.
以下似乎工作正常。
Attempting JOIN with OR : 用OR尝试加入:
select count(distinct `TableA`.AccountID) from `TableA`
JOIN TableB ON (TableA.AccountID = TableB.AccountID_1
OR TableA.AccountID = TableB.AccountID_2);
Attempting SQL UNION 尝试SQL UNION
select count(distinct b.AccountID) from (
select `AccountID` from TableA
join TableB on TableA.AccountID = TableB.AccountID_1
union
select `AccountID` from TableA
join TableB on TableA.AccountID = TableB.AccountID_2
) as b;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.