title: Dynamic query in PHP/POSTGRES : DATA and METADATA
body: <p>I have a problem I can not solve and found no solutions on the web.
  I have two database tables (in postgresql). </p>
  
  <p><strong>Table_A is the real dataset:</strong></p>
  
  <pre><code>date; var_1; var_2; var_3; var_4; var_5; ....; var_100
  03-10-14; A; 15; B; 10; 7; ...; TT
  04-10-14; U; 67; G; 9; 99; ...; LLK
  etc 
  etc
  </code></pre>
  
  <p><strong>Table_B is the "metadata":</strong></p>
  
  <pre><code>var; long_name; type; use;
  date; Date of transaction; DATE; yes
  var_1; Type of customer; ATTRIBUTE; yes
  var_2; Age; NUMBER; yes
  etc 
  ect
  </code></pre>
  
  <p>So each column of table_A has the metadata definied in a row of table_B</p>
  
  <p>Now the problem:</p>
  
  <p>I want to write a query in php that returns the data of table_A with ONLY the column defined as DATE in table_B and each one of the columns defined as ATTRIBUTE en table_B.</p>
  
  <p>Obviously I want to perform this dynamically (I do not want to write the query when the tables change...)</p>
  
  <p>I tried many approaches I succeeded with the DATE column and ONE ATTRIBUTE, but not all.</p>
  
  <p>Any ideas?
  THANKS in advance</p>