Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way is to create a new table containing the common fields from the three other tables and add an index on the common date field. The original three tables should contain a foreign key linking to the common table. With this design the query becomes simple:</p> <pre><code>SELECT * FROM common_table ORDER BY "date" DESC LIMIT 100 </code></pre> <p>If you also need data from the more specific tables you can use LEFT JOINs to also select that data in the same query.</p> <p>If you can't change your design and performance is not an issue then you can use UNION ALL to combine the results from all three tables before sorting:</p> <pre><code>SELECT * FROM table1 UNION ALL SELECT * FROM table2 UNION ALL SELECT * FROM table3 ORDER BY "date" DESC LIMIT 100 </code></pre> <p>Note that the above will only work if all tables have the same structure. If you have fields that occur in one table but not in others then you should omit them from the SELECT or else return NULL for that column in the other tables. For example if:</p> <ul> <li><code>table1</code> has columns <code>a</code>, <code>b</code>, <code>c</code> and <code>date</code>.</li> <li><code>table2</code> has columns <code>b</code>, <code>c</code> and <code>date</code>.</li> <li><code>table3</code> has columns <code>a</code>, <code>c</code> and <code>date</code>.</li> </ul> <p>Then use this:</p> <pre><code>SELECT a, b, c, "date" FROM table1 UNION ALL SELECT NULL AS a, b, c, "date" FROM table2 UNION ALL SELECT a, NULL as b, c, "date" FROM table3 ORDER BY "date" DESC LIMIT 100 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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