title: SQL: Selecting columns based on column value from another table
body: <p>I have the following tables:</p>
  
  <pre><code>UserPrivileges: 
  +--------+------+------+------+
  | UserID | Col1 | Col2 | Col3 |
  +--------+------+------+------+
  |      1 |    0 |    1 |    1 |
  |      2 |    0 |    0 |    1 |
  |      3 |    1 |    0 |    0 |
  |      4 |    1 |    1 |    0 |
  +--------+------+------+------+
  
  Data:
  +--------+------+------+------+
  | DataID | Col1 | Col2 | Col3 |
  +--------+------+------+------+
  |      1 | A    | B    | C    |
  |      2 | D    | E    | F    |
  |      3 | G    | H    | I    |
  |      4 | J    | K    | L    |
  +--------+------+------+------+
  </code></pre>
  
  <p>My question at its simplest form doesn't has anything to do with the <code>Data</code> table but I just explain it anyways so that I might be doing it the wrong way.</p>
  
  <p>How would I select the Column names from UserPrivileges based on the value ? So that I can use the result in another query to select only those columns.</p>
  
  <p>Something along these lines:</p>
  
  <p><code>SELECT (COLUMNS_NAME_QUERY_FROM_UserPrivileges(UserID='#')) WHERE DataID = '#' FROM Data</code></p>
  
  <p>Or I don't mind a better way to manage user Privileges for specific columns.</p>