Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to effectively join 3 tables M:N
    primarykey
    data
    text
    <p>I have few tables:</p> <p><strong>Apps</strong>:</p> <pre><code>id | name | url_key =================== 1 | Hello World | hello-world 2 | Snake 2 | snake-2 </code></pre> <p><strong>Developers</strong>:</p> <pre><code>id | name | url_key =================== 1 | Mr. Robinson | mr-robinson 2 | Richard | richard 3 | Nokia | nokia </code></pre> <p><strong>Apps-Developers-Assignment</strong></p> <pre><code>app_id | developer_id ===================== 1 | 1 1 | 2 2 | 3 </code></pre> <p>So as you may see, one app may have more than one developer.</p> <p>What i actually need is to write a PostgreSQL query to select all apps with some usefull information about related developers. e.g. sth like this:</p> <pre><code>Hello World | 1-hello-world | Mr.Robinson&lt;1-mr-robinson&gt; /and 1 more/ Snake 2 | 2-snake-2 | Nokia&lt;3-nokia&gt; </code></pre> <p>So if the app has one developer, just append the developer's name, url key and id to the app, if it has more developers, append the first and append the information of "how many developers are there".</p> <p>And what's most important - <strong>i need to eventually filter the results</strong> (e.g. not return apps from some specified developer).</p> <p>I know it can be solved by write first query for apps itself and then send next query per each row, but not the nice way, i think...</p> <p>If i just simply JOIN the table:</p> <pre><code>SELECT * FROM apps a LEFT JOIN apps_developers_assignment ada ON a.id = ada.app_id LEFT JOIN developers d ON ada.developer_id = d.id Hello World | 1-hello-world | Mr.Robinson&lt;1-mr-robinson&gt; Hello World | 1-hello-world | Richard&lt;2-richard&gt; Snake 2 | 2-snake-2 | Nokia&lt;3-nokia&gt; </code></pre> <p>It returns duplicate apps... and i don't know how to filter these results by developer (as i wrote above).</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.
 

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