可能的重复: 为什么有人会在SQL子句中使用WHERE 1=1 AND <条件> ?

我看到一些人使用语句来查询MySQL数据库中的表,如下所示:

select * from car_table where 1=1 and value="TOYOTA"

但是这里1=1是什么意思呢?


当前回答

通常是在人们建立SQL语句的时候。

当你加上and value = "Toyota"时,你不必担心前面是否有条件,或者只是WHERE。优化器应该忽略它

没有魔法,只是实用


示例代码:

commandText = "select * from car_table where 1=1";

if (modelYear <> 0)     commandText += " and year="+modelYear
if (manufacturer <> "") commandText += " and value="+QuotedStr(manufacturer)
if (color <> "")        commandText += " and color="+QuotedStr(color)
if (california)         commandText += " and hasCatalytic=1"

否则你就得有一套复杂的逻辑:

commandText = "select * from car_table"
whereClause = "";
if (modelYear <> 0)
{
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "year="+modelYear;
}
if (manufacturer <> "")
{    
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "value="+QuotedStr(manufacturer)
}
if (color <> "")
{
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "color="+QuotedStr(color)
}
if (california)
{
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "hasCatalytic=1"
}

if (whereClause <> "")
   commandText = commandText + "WHERE "+whereClause;

其他回答

该查询查找1 = 1且value = 'TOYOTA'的所有行。因此,在这种情况下,它是无用的,但如果您省略了WHERE语句,使用WHERE 1=1来提醒您选择不使用WHERE子句可能是一个好主意。

通常是在人们建立SQL语句的时候。

当你加上and value = "Toyota"时,你不必担心前面是否有条件,或者只是WHERE。优化器应该忽略它

没有魔法,只是实用


示例代码:

commandText = "select * from car_table where 1=1";

if (modelYear <> 0)     commandText += " and year="+modelYear
if (manufacturer <> "") commandText += " and value="+QuotedStr(manufacturer)
if (color <> "")        commandText += " and color="+QuotedStr(color)
if (california)         commandText += " and hasCatalytic=1"

否则你就得有一套复杂的逻辑:

commandText = "select * from car_table"
whereClause = "";
if (modelYear <> 0)
{
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "year="+modelYear;
}
if (manufacturer <> "")
{    
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "value="+QuotedStr(manufacturer)
}
if (color <> "")
{
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "color="+QuotedStr(color)
}
if (california)
{
   if (whereClause <> "") 
      whereClause = whereClause + " and ";
   commandText += "hasCatalytic=1"
}

if (whereClause <> "")
   commandText = commandText + "WHERE "+whereClause;

当我需要动态应用过滤器时,我就这样做了。 比如,在编码时,我不知道有多少过滤器用户将应用(fld1 = val1和fld2=val2和…) 因此,为了重复语句“and FLD = val”,我从“1 = 1”开始。 因此,我不需要修饰语句中的第一个“和”。

1=1的条件总是为真因为总是1等于1,所以这个表述总是为真。 但有时它毫无意义。但其他时候,当动态生成where条件时,开发人员使用这种方法。

例如,让我们看看这段代码

<?php
//not that this is just example
//do not use it like that in real environment because it security issue.
$cond = $_REQUEST['cond'];
if ($cond == "age"){
 $wherecond = " age > 18";
}         
$query = "select * from some_table where $wherecond";
?>

因此,在上面的例子中,如果$_REQUEST['cond']不是"age",查询将返回mysql错误,因为在where条件之后什么都没有。

查询将被select * from some_table where,这是错误

为了修复此问题(至少在这个不安全的示例中),我们使用

<?php
//not that this is just example
//do not use it like that in real environment because it security issue.
$cond = $_REQUEST['cond'];
if ($cond == "age"){
 $wherecond = " age > 18";
} else {
 $wherecond = " 1=1";
}        
$query = "select * from some_table where $wherecond";
?>

所以现在如果$_REQUEST['cond']没有age, $wherecond将是1=1,所以查询将不会有mysql错误返回。

查询将select * from some_table where 1=1,避免mysql错误

希望你能理解我们使用1=1,同时注意上面的例子不是真实世界的例子,它只是向你展示这个想法。

这只是一个永远正确的表达。有些人把它作为一种变通方法。

它们有一个静态语句:

select * from car_table where 1=1

所以他们现在可以在where子句with中添加一些东西

and someother filter