这是一个语义正确的SQLite搜索查询吗?

时间:2014-12-05 10:23:46

标签: mysql database html5 sqlite

编辑** 不确定此附加信息是否有帮助,但试图保持简单。

用户通过html表单选择数据(“性别”最多三个选项,“颜色”最多六个,然后可以选择单个价格范围),然后在jquery函数中处理此数据构造下面的字符串并将其保存在变量中。然后将该变量用作sql查询,下面显示的查询字符串是此变量的输出。

我正在使用chrome的开发人员工具进行调试,没有错误显示**


此查询字符串有什么问题吗?

'SELECT * FROM flipflops WHERE colour="black" OR colour="brown" AND gender="mens" OR gender="womens" AND price<=20'

值正确,因为它们与db匹配。说实话,它似乎回归了它喜欢的东西。我希望有人能够明白我做错了什么,因为我是一个无知的新手。

任何帮助都将非常感谢! :d


编辑** 现在已被替换,当您按原样复制时,其效果更完美!

'SELECT * FROM flipflops WHERE colour IN ("white","navy","blue") AND gender IN ("mens","juniors") AND price >= 20 AND price <= 30'

我不得不使用RegExp在jquery中创建字符串,如下所示 现在忽略了价格范围但它没有从db返回任何内容而我没有得到控制台错误

var regExp = /\(([^)]+)\)/;

var queryValue = regExp.exec("SELECT * FROM flipflops WHERE colour IN /(\'" + colourValues.join("','") + "/')\ AND gender IN /(\'" + genderValues.join("','") + "/')\"");

输出到控制台值取决于表单值的选择(对不起,这很复杂,我太乱了!)

    Array[2]
0: "('white','blue/')"
1: "'white','blue/'"
index: 41input: 
"SELECT * FROM flipflops WHERE colour IN /('white','blue/') AND gender IN /('womens','juniors/')""
length: 2

2 个答案:

答案 0 :(得分:1)

你可能错过了一个括号吗?

SELECT * FROM flipflops 
WHERE
( 
  colour="black" 
  OR colour="brown"
) 
AND 
(
  gender="mens" 
  OR gender="womens"
)
AND price<=20

如果是这种情况你也可以这样写:

SELECT * FROM flipflops 
WHERE colour IN ("black","brown")
AND gender IN ("mens","womens")
AND price<=20

答案 1 :(得分:0)

以下代码使用Arions IN()运算符示例创建动态查询。查询是根据点击的框等构建的。

<form action="#" method="post">
<input type="checkbox" name="color[]" value="Red"><label>Red</label><br/>
<input type="checkbox" name="color[]" value="Blue"><label>Blue</label><br/>
<input type="checkbox" name="color[]" value="Brown"><label>Brown</label><br/>
<!--input type="checkbox" name="gender[]" value="Any"><label>Any</label><br/-->
<br>
<input type="checkbox" name="gender[0]" value="Male"><label>Male</label><br/>
<input type="checkbox" name="gender[1]" value="Female"><label>Female</label><br/>
<br>
<input type="text" name="cost" ><label>Cost</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
// Set up stub of query
$sql = "SELECT * FROM flipflops ";
//Set initial WHERE clause
$clause ="WHERE ";

if(isset($_POST['submit'])){//to run PHP script on submit
//Color set up IN() operator
if(!empty($_POST['color'])){
    $sql .= $clause." colour IN (";
    // Loop to store and display values of individual checked checkbox.
    foreach($_POST['color'] as $color){
    $sql .= $color.",";
    }
    $sql = rtrim($sql, ",");
    $sql .= ")";
    //Set to AND after initial WHERE
    $clause =" AND ";
}
//Gender show only one gender, if 2 genders clicked  all 2 genders are selected
if(!empty($_POST['gender']) && count($_POST['gender']) == 1){
    // Loop to store and display values of individual checked checkbox.
    foreach($_POST['gender'] as $gender){
        $sql .= $clause. "gender = ".$gender;
    }
    $clause =" AND ";
}
//Cost
if(!empty($_POST['cost'])){
    $sql .= $clause." cost = ". $_POST['cost'];
    }
}
echo $sql;
?>