title: SQL : Create a full record from 2 tables
body: <p>I've got a DB structure as is (simplified to maximum for understanding concern):</p>
  
  <pre><code>Table "entry" ("id" integer primary key)
  Table "fields" ("name" varchar primary key, and others)
  Table "entry_fields" ("entryid" integer primary key, "name" varchar primary key, "value")
  </code></pre>
  
  <p>I would like to get, for a given "entry.id", the detail of this entry, ie. all the "entry_fields" linked to this entry, in a single SQL query.</p>
  
  <p>An example would be better perhaps: </p>
  
  <p>"fields": </p>
  
  <pre><code>"result"
  "output"
  "code"
  "command"
  </code></pre>
  
  <p>"entry" contains:</p>
  
  <pre><code>id : 842
  id : 850
  </code></pre>
  
  <p>"entry_fields" contains: </p>
  
  <pre><code>entryid : 842, name : "result", value : "ok"
  entryid : 842, name : "output", value : "this is an output"
  entryid : 842, name : "code", value : "42"
  entryid : 850, name : "result", value : "ko"
  entryid : 850, name : "command", value : "print ko"
  </code></pre>
  
  <p>The wanted output would be:</p>
  
  <pre><code>| id  | command    | output               | code | result |
  | 842 | NULL       | "this is an output"  | 42   | ok     |
  | 850 | "print ko" | NULL                 | NULL | ko     |
  </code></pre>
  
  <p>The aim is to be able to add a "field" without changing anything to "entry" table structure</p>
  
  <p>I tried something like: </p>
  
  <pre><code>SELECT e.*, (SELECT name FROM fields) FROM entry AS e
  </code></pre>
  
  <p>but Postgres complains:</p>
  
  <blockquote>
    <p>ERROR:  more than one row returned by a subquery used as an expression</p>
  </blockquote>
  
  <p>Hope someone can help me!</p>