<p>Suppose we have a table A:</p>

<pre><code>itemid mark
1      5
2      3
</code></pre>

<p>and table B:</p>

<pre><code>itemid mark
1      3
3      5
</code></pre>

<p>I want to join A*B on A.itemid=B.itemid both right and left ways. i.e. result:</p>

<pre><code>itemid A.mark B.mark
1      5      3
2      3      NULL
3      NULL   5
</code></pre>

<p>Is there a way to do it in one query in MySQL?</p>

Could do with some work but here is some sql
```
```
select distinct T.itemid, A.mark as "A.mark", B.mark as "B.mark"
    from (select * from A union select * from B) T 
    left join A on T.itemid = A.itemid 
    left join B on T.itemid = B.itemid;
```
```
This relies on the left join, which returns all the rows in the original table (in this case this is the subselect table T). If there are no matches in the joined table, then it will set the column to NULL.