title: How to filter columns of one table by values given as rows in another table?
body: <p>I have two tables in MySQL. I would like to select columns from <em>main_table</em> which are given as rows in <em>cols_filter</em> table (it is a small table which can be edited at any time).</p>
  <p><strong>main_table:</strong></p>
  <pre><code>+--------+--------+---------+-------+-------+
  | Col_A  | Col_B  |  Col_C  | Col_D | Col_E |
  +--------+--------+---------+-------+-------+
  | Apple  | 585416 | Monday  |   0   |   Y   |
  | Banana | 857463 | Sunday  |   1   |   N   |
  | Orange | 852147 | Friday  |   0   |   N   |
  | Plum   | 753951 | Sunday  |   1   |   Y   |
  | Peach  | 448691 | Monday  |   0   |   N   |
  +--------+--------+---------+-------+-------+
  </code></pre>
  <p><strong>cols_filter:</strong></p>
  <pre><code>+--------+
  | names  |
  +--------+
  | Col_A  |
  | Col_B  |
  | Col_E  |
  +--------+
  </code></pre>
  <p><strong>Expected output:</strong></p>
  <pre><code>+--------+--------+-------+
  | Col_A  | Col_B  | Col_E |
  +--------+--------+-------+
  | Apple  | 585416 |   Y   |
  | Banana | 857463 |   N   |
  | Orange | 852147 |   N   |
  | Plum   | 753951 |   Y   |
  | Peach  | 448691 |   N   |
  +--------+--------+-------+
  </code></pre>
  <p>I was trying to create a subquery with GROUP_CONCAT to have all rows from <em>cols_filter</em> as comma seperated values in one line:</p>
  <pre><code>SELECT GROUP_CONCAT(names) AS Columns
  FROM cols_filter
  
  +---------------------+
  |       Columns       |
  +---------------------+
  | Col_A, Col_B, Col_E |
  +---------------------+
  </code></pre>
  <p>... and using it as a list in main query instead of putting certain column names there, but it did not work.</p>
  <p>I was not able to find a similar question here. Any ideas?</p>