首页 文章

“其中1 = 1”声明[重复]

提问于
浏览
188

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

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

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

1=1 在这里意味着什么?

10 回答

  • 13

    它只是一个永远真实的表达 . 有些人用它作为解决方法 .

    他们有一个静态语句,如:

    select * from car_table where 1=1
    

    所以他们现在可以在where子句中添加一些内容

    and someother filter
    
  • 0

    如果该查询是动态构建的,原始作者可能不想考虑一组空条件,因此以这样的结尾:

    sql = "select * from car_table where 1=1"
    for each condition in condition_set
    
        sql = sql + " and " + condition.field + " = " + condition.value
    
    end
    
  • 48

    在动态传递条件时,使用此函数会出现在复杂查询中,您可以使用“AND”字符串连接条件 . 然后,不是计算您传入的条件数,而是在库存SQL语句的末尾放置一个“WHERE 1 = 1”并抛出连接条件 .

    不需要使用1 = 1你可以使用0 = 0 2 = 2,3 = 3,5 = 5 25 = 25 ......

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

    在这里你也会得到相同的结果,如1 = 1条件

    因为所有这些案例总是真实的表达

    1=1 is alias for true
    
  • 301

    通常当人们 Build 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;
    
  • 31

    查询查找1等于1且值等于'TOYOTA'的所有行 . 所以在这种情况下它是无用的,但如果省略WHERE语句,最好使用WHERE 1 = 1来提醒您选择不使用WHERE子句 .

  • 1

    1=1 将始终为true,因此 value="TOYOTA" 位是重要的位 .

    您可以在以下几种情况下获得:

    生成的SQL:如果你没有添加第一个条件,那么创建一个复杂的 where 语句会更容易,因此通常会在开头放置 1=1 ,并且所有其他条件都可以附加 And

    调试:有时您会看到人们在where条件的顶部放入 1=1 ,因为它使他们能够在调试查询时自由地切换和更改其余条件 . 例如

    select * from car_table
    where 1=1
    --and value="TOYOTA"
    AND color="BLUE"
    --AND wheels=4
    

    必须要说的是,这不是特别好的做法,通常不应该出现在 生产环境 代码中 . 它甚至可能无法帮助优化查询 .

  • 36

    除了所有其他答案,这是一个简单的SQL injection attacks技术 . 如果您向某个SQL添加 OR where 1=1 语句,那么由于表达式的固有真实性,它将返回所有结果 .

  • 4

    1 = 1其中condition始终为true,因为always 1等于1,因此该语句将始终为true . 虽然它有时没有任何意义 . 但其他时候开发人员在动态生成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,同时注意上面的例子不是现实世界的例子,它只是为了向你展示这个想法 .

  • 4

    大多数时间开发人员使用这些类型的查询,如果他正在开发查询构建器类型应用程序或构建一些复杂的SQL查询,那么与select语句字符串一起添加条件子句其中1 = 1,并且在程序中不需要添加任何检查它 .

  • 2

    当我需要动态应用过滤器时,我这样做了 .
    比如,虽然编码我不知道将应用多少过滤器用户(fld1 = val1和fld2 = val2和...)
    所以,重复声明"and fld = val"我以"1 = 1"开头 .
    因此,我不需要修改声明中的第一个"and " .

相关问题