Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Only with dynamic SQL. It is also possible to left join many different tables and use CASE based on type, but the tables would be all have to be known in advance.</p> <p>It would be easier to recommend an appropriate design if we knew more about what you are trying to achieve, what your design currently looks like and why you've chosen that particular table design in the first place.</p> <pre><code>Say you have a table of foods: id INT foodtype VARCHAR(50) (right now it just contains 'hotdog' or 'hamburger') name VARCHAR(50) Then hotdogs: id INT length INT width INT Then hamburgers: id INT radius INT thickness INT </code></pre> <p>Normally I would recommend some system for constraining only one auxiliary table to exist, but for simplicity, I'm leaving that out.</p> <pre><code>SELECT f.*, hd.length, hd.width, hb.radius, hb.thickness FROM foods f LEFT JOIN hotdogs hd ON hd.id = f.id AND f.foodtype = 'hotdog' LEFT JOIN hamburgers hb ON hb.id = f.id AND f.foodtype = 'hamburger' </code></pre> <p>Now you will see that such a thing can be code generated (or even for a very slow prototype dynamic SQL on the fly) from <code>SELECT DISTINCT foodtype FROM foods</code> given certain assumptions about table names and access to the table metadata.</p> <p>The problem is that ultimately whoever consumes the result of this query will have to be aware of new columns showing up whenever a new table is added.</p> <p>So the question moves back to your client/consumer of the data - how is it going to handle the different types? And what does it mean for different types to be in the same set? And if it needs to be aware of the different types, what's the drawback of just writing different queries for each type or changing a manual query when new types are added given the relative impact of such a change anyway?</p>
    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. This table or related slice is empty.
    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