title: Mysql filter query based on user inputs
body: <p><a href="https://stackoverflow.com/questions/43634385/mysql-select-query-based-on-multiple-user-inputs">My previous question</a> was not giving a clear idea about my problem, that is why I am asking a new question .</p>
  
  <p>Here is my table structure </p>
  
  <pre><code>id,
  subject (varchar),
  category(varchar),
  medium(json),
  material(json),
  style(json)
  </code></pre>
  
  <p>User can able to search based on the values in table fields ie if user choose only <code>subject</code> (he can select it from multiple select dropdown) query result contains rows that matches the selected items.</p>
  
  <p>If user selected subject and category query result contains rows that matches the selected items and so on (in short it's a filter search like shopping websites)</p>
  
  <p>The problem is I don't how I can efficiently build the MySQL query based on these inputs because some of the Mysql columns are JSON.</p>
  
  <p>Here is the separate SQL query:</p>
  
  <pre><code>// select rows that contains subject test or test1
  SELECT * 
  FROM `cushbu_art` 
  WHERE subject IN ('test','test1')
  
  // select rows that contains medium is paper or food
  SELECT * 
  FROM `cushbu_art` 
  WHERE (JSON_CONTAINS(medium, '["paper"]') 
         OR JSON_CONTAINS(medium, '["food"]'))
  </code></pre>
  
  <p>Here is a sample input with two parameters (<code>subject</code> and <code>medium</code>)</p>
  
  <pre><code>{
      "subject":["test","test1"],
      "medium":["paper","wood"],
      "category":"",
      "material":"",
      "style":""
  }
  </code></pre>
  
  <p>Note: input and output are in JSON (REST API)</p>
  
  <p><strong>db.js</strong></p>
  
  <pre><code>function(req, res) {
      var sql = '';
  
      if(req.body.subject) {
          sql+=//PREPARE SUBJECT QUERY
      }
  
      if(req.body.medium) {
         sql+=//PREPARE QUERY
      }
  
      //FINAL SQL QUERY HERE;
      db.query(sql, function(error, result) {
         console.log(result);
      });
  }
  </code></pre>