Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>IMHO you either join your tables if they're related to each other. One way to do it if <code>col1</code> is the common column </p> <pre><code>SELECT COALESCE(m.col1, d.col1, e.col1) column_name1, m.col2 column_name2, m.col3 column_name3, d.col2 column_name4, e.col2 column_name5 FROM messages m FULL JOIN messagedata d ON m.col1 = d.col1 FULL JOIN extradata e ON d.col1 = e.col1 </code></pre> <p>Sample output:</p> <pre> | COLUMN_NAME1 | COLUMN_NAME2 | COLUMN_NAME3 | COLUMN_NAME4 | COLUMN_NAME5 | |--------------|--------------|--------------|--------------|--------------| | 1 | value12 | value13 | data1 | extradata1 | | 2 | value21 | value31 | data2 | extradata2 | </pre> <p>or you <code>UNION</code> them in this way</p> <pre><code>SELECT col1 column_name1, col2 column_name2, col3 column_name3 FROM messages UNION ALL SELECT col1, col2, NULL FROM messagedata UNION ALL SELECT col1, NULL, NULL FROM extradata </code></pre> <p><strong>Note</strong> that the number of columns and their respective data types should be the same for all <code>SELECT</code>s that you <code>UNION</code>. To overcome this you can inject constant values (e.g. <code>NULL</code>) and convert/cast column values to some common type (e.g. <code>VARCHAR2</code>) if needed.</p> <p>Sample output:</p> <pre> | COLUMN_NAME1 | COLUMN_NAME2 | COLUMN_NAME3 | |--------------|--------------|--------------| | 1 | value12 | value13 | | 2 | value21 | value31 | | 1 | data1 | (null) | | 2 | data2 | (null) | | 1 | (null) | (null) | | 2 | (null) | (null) | </pre> <p>Here is <strong><a href="http://sqlfiddle.com/#!4/83cea/3" rel="nofollow">SQLFiddle</a></strong> demo</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload