Wednesday 26 April 2017

mysql filter query based on user inputs

My previous question not giving clear idea about my problem that why asking a new question

Here is my table structure

1.id,subject(varchar),category(varchar),medium(json),material(json),style(json).

User can able to search based on the values in table fields ie if user choose only subject (he can select it from multiple select dropdown) query result contains rows that matches the selected items. if user selected subject and category query result contains rows that matches the selected items and so on (in short its filter search like shopping websites)

The problem is i don't how can i efficiently build the MySQL query based on these inputs because some of the mysql fields are json Here is the separate SQL query's

//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"]')

)

Here is a sample input with two parameters (subject and medium)

{
    "subject":["test","test1"],
    "medium":["paper","wood"],
    "category":"",
    "material":"",
    "style":""
}

Note: Input and output are in JSON (REST API)

db.js

function(req,res){
     var sql = '';
    if(req.body.subject){
        sql+=//PREPARE SUBJECT QUERY

      }
    if(req.body.medium){
       sql+=//PREPARE QUERY
      }
   //FINAL SQL QUEREY HERE';
   db.query(sql,function(error,result){
       console.log(result);
    });

  }



via Jabaa

No comments:

Post a Comment