MySQL UNION vs JOIN with OR in clause
<p>I have one table <code>TableA</code> with columns <code>[AccountID, Email]</code>. I have another table <code>TableB</code> with <code>[AccountID_1, AccountID_2, email]</code>. I need to match the <code>AccountID</code> in <code>TableA</code> with either of the <code>AccountIDs</code> in <code>TableB</code>. 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 <code>"Can't reopen table: TableB".</code></p>

<h1>Attempting JOIN with OR</h1>

<pre><code>select count(distinct TableA.id) from TableA
    JOIN TableB ON TableB.AccountID = TableA.AccountID_1 
    OR TableB.AccountID = TableA.AccountID_2
;
</code></pre>

<h1>Attempting sql UNION</h1>

<pre><code>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;
</code></pre>