基于多个用户输入的Mysql select查询

时间:2017-04-26 12:35:45

标签: mysql express

目前我的表格包含以下字段

id,medium(json) ,material(json),subject,category

我想基于用户输入执行过滤器,如果特定字段上存在用户输入,则根据该输入执行where sql

最小代码

var sql = 'SELECT * FROM table';

    //category is present in userinput 
    if(category){
      sql+='WHERE category=category';   
    }
    //subject is present in userinput
    if(subject){
      sql+='WHERE subject=subject'
    }
    if(material){
       sql+='WHERE material=material'
     }
   if(medium){
      sql+='WHERE medium=medium'
    }

    db.query(sql,function(error,result){
         console.log(result);
    });

但我很困惑,当有任何两个用户输入存在或用户输入包含所有字段时,我可以在{sql}查询中放置AND

4 个答案:

答案 0 :(得分:1)

试试这个:

    var sql = 'SELECT * FROM table';
    var where = ' WHERE';

    //category is present in userinput 
    if(category){
      sql += where + ' category=category';   
      where = ' and';
    }

    //subject is present in userinput
    if(subject){
      sql += where + ' subject=subject';
      where = ' and';
    }

    if(material){
       sql += where + ' material=material';
       where = ' and';
     }

    if(medium){
      sql += where + ' medium=medium';
    }

    db.query(sql,function(error,result){
         console.log(result);
    });

答案 1 :(得分:1)

编辑#3:

现在,我做对了(我想:>)。

var sql = 'SELECT * FROM table WHERE 1';

//category is present in userinput 
if(category) {
  sql+=' AND category = category';   
}
//subject is present in userinput
if(subject) {
  sql+=' AND subject = subject'
}
if(material) {
  sql+=' AND JSON_SEARCH(material, "one", material) IS NOT NULL'
}
if(medium) {
  sql+=' AND JSON_SEARCH(medium, "one", medium) IS NOT NULL'
}

请关注正确的报价。此外,更改变量名称可能是个好主意,因此它们 NOT 与表列名称相对应。顺便说一句。 subject medium 是保留名称,最好将它们放在反引号中。

截图: New screenshot

编辑:

抱歉,我误解了你的问题。使用JSON_EXTRACT从json列 json_column 中提取数据,假设您有一维json数据结构,如

{
  "category": "Test Category"
}

你明白了。

var sql = 'SELECT * FROM table WHERE 1';

//category is present in userinput 
if(category) {
  sql+=' AND category = category';   
}
//subject is present in userinput
if(subject) {
  sql+=' AND subject = subject'
}
if(material) {
  sql+=' AND JSON_EXTRACT(material, "$.json_column") = material'
}
if(medium) {
  sql+=' AND JSON_EXTRACT(medium, "$.json_column") = medium'
}

编辑#2:从phpMyAdmin添加了截图: pma screenshot json_extract

答案 2 :(得分:1)

尝试以下逻辑为您的查询获取here

的参考
 if(category){
              sql[] ='category=category';   
            }

            if(subject){
              sql[] ='subject=subject'
            }
            if(material){
               sql[] ='material=material'
             }
           if(medium){
              sql[] ='medium=medium'
            }
    var sql1 = 'SELECT * FROM table';

    if (index < sql.length) {
        sql1 += ' WHERE ' + sql.join(' AND ');
    }

答案 3 :(得分:0)

var sql = 'SELECT * FROM table';
var $aux = [];

//category is present in userinput 
if(category){
  $aux[] ='category=category';   
}
//subject is present in userinput
if(subject){
  $aux[]='subject=subject'
}
if(material){
   $aux[]='material=material'
 }
if(medium){
  $aux[]='medium=medium'
}

$wheres = implode(' and ', $aux);

if($wheres)
    $sql+= ' WHERE '.$wheres;

db.query(sql,function(error,result){
     console.log(result);
});