211

Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?

I saw some people use a statement to query a table in a MySQL database like the following:

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

But what does 1=1 mean here?

Community
  • 1
  • 1
Mellon
  • 37,586
  • 78
  • 186
  • 264
  • 9
    Note that in MySQL, just `WHERE 1 and ...` would work as well... – Konerak Nov 16 '11 at 09:58
  • 6
    it is a alias for `true` – Vineet Menon Nov 16 '11 at 10:05
  • 42
    also used in sqlinjection attempts – talha2k Nov 16 '11 at 11:58
  • 18
    Also note that SELECT * FROM myTable WHERE 1 = 2 is a very basic and generic way of fetching the column schema of a table :) (Just in case there are no other options available) – Christian Nov 16 '11 at 13:56
  • 18
    It prevents your query from running in the alternate universe where 1=2. – Brian White Nov 29 '12 at 22:44
  • @Christian: what do you mean by table schema here because when I execute that query, I just see empty resultset with column name of that table. Is it the same you mean table schema! – ursitesion Dec 13 '13 at 13:48
  • @ursitesion That's the whole point, to get no rows but all columns of that table. Executing such a query e.g. via ADO.NET, you get a DataTable with a Columns collection where you can check out some properties of the columns :) – Christian Jan 08 '14 at 21:06

10 Answers10

339

It's usually when folks build up SQL statements.

When you add and value = "Toyota" you don't have to worry about whether there is a condition before or just WHERE. The optimiser should ignore it

No magic, just practical


Example Code:

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"

Otherwise you would have to have a complicated set of logic:

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;
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
gbn
  • 422,506
  • 82
  • 585
  • 676
  • 2
    Awesome. Now I can build my complex queries with strings rather than arrays ;). I did, however, want to verify with an EXPLAIN query whether it triggered "Using Where." Indeed, it does not. – landons Nov 16 '11 at 11:57
  • 74
    Though more experienced programmers would do something like `" and ".Join(conditions)` to generate the where statement... or use an O/RM... – BlueRaja - Danny Pflughoeft Nov 16 '11 at 15:05
  • 4
    If you were building stuff of "or"s you would instead start with "where false " or "where 1=0 ". – Thomas Ahle Nov 30 '11 at 00:18
  • 2
    The problem with `conditions.join(" and ")` is that if `conditions.length == 0` you end up with a dangling `where`. So experienced programmers would actually use `1=1`. – Sarsaparilla Apr 14 '21 at 01:01
49

If that query is being built dynamically, original author probably doesn't want to consider an empty set of conditions, so ends with something like this:

sql = "select * from car_table where 1=1"
for each condition in condition_set

    sql = sql + " and " + condition.field + " = " + condition.value

end
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • 29
    IMO this is just bad coding. Personally I store all conditions in an array then stick them together with "AND". – DisgruntledGoat Nov 16 '11 at 10:26
  • 4
    I believe it would more like `" AND "` – V4Vendetta Nov 16 '11 at 10:57
  • 12
    @DisgruntledGoat - And that's absolutely a better way to do things; but sometimes you don't have the luxury of easy to manipulate arrays and array join statements. – Ben Walding Nov 16 '11 at 10:58
  • 2
    @BenW You don't need easy to manipulate arrays and array join statements to do `if (first) { s += " WHERE"; first = false; } else { s += " AND"; }` – Orion Adrian Nov 16 '11 at 16:00
  • 3
    Storing an array of database filter conditions in programming code is a *good* development practice? "where 1=1" is a classic dynamic sql in a stored proc pattern. – Brian White Nov 29 '12 at 22:44
37

1=1 will always be true, so the value="TOYOTA" bit is the important one.

You get this in a few scenarios including:

Generated SQL: It's easier to create a generate a complex where statement if you don't have to work out if you're adding the first condition or not, so often a 1=1 is put at the beginning, and all other conditions can be appended with an And

Debugging: Sometimes you see people put in a 1=1 at the top of a where condition as it enables them to freely chop and change the rest of the conditions when debugging a query. e.g.

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

It has to be said that it isn't particularly good practice and normally shouldn't occur in production code. It may even not help the optimization of the query very much.

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
30

As well as all the other answers, it's a simple technique for SQL injection attacks. If you add a OR where 1=1 statement to some SQL then it's going to return all the results due to the inherent truthiness of the expression.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • I can't see how this is related to the question. adding 'OR 1=1' (I guess the OR where was a typo) through SQLi will return all results no matter if you have "where 1=1 and cond1" or just "where cond1". – Filipe Pina Nov 16 '11 at 11:25
  • 5
    The question was "what does `1=1` mean" and I just thought it'd be a useful example to show how it can be used. – Jeff Foster Nov 16 '11 at 11:30
  • I agree that it's a useful note. If someone comes across the question of why am I seeing 1=1 they should always consider the possibility it's a user crafted SQL injection. – Steve Smith Nov 16 '11 at 11:39
  • Sorry, I didn't notice it was about the 1=1 read from logs but instead thought it was only about why using 1=1 AND something. Indeed, it's a useful within that context. – Filipe Pina Nov 16 '11 at 15:10
  • +1 for OR. I didn't understand the other SQL injection answer until I read this. –  Nov 16 '11 at 16:14
17

Its just an always true expression. Some people use it as an work-around.

They have a static statement like:

select * from car_table where 1=1

So they can now add something to the where clause with

and someother filter
Udo Held
  • 12,314
  • 11
  • 67
  • 93
4

the 1=1 where condition is always true because always 1 is equal 1 , so this statement will be always true. While it means nothing sometimes. but other times developers uses this when the where condition is generated dynamically.

for example lets see this code

<?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";
?>

so in the above example if the $_REQUEST['cond'] is not "age" the query will return mysql error because there are nothing after the where condition.

the query will be select * from some_table where and that is error

to fix this issue (at least in this insecure example) we use

<?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";
?>

so now if the $_REQUEST['cond'] is not age the $wherecond will be 1=1 so the query will not have mysql error return.

the query will be select * from some_table where 1=1 and that avoid the mysql error

hope you understand when we use 1=1 while note that the above example is not real world example and it just to show you the idea.

Mohammed Shannaq
  • 806
  • 3
  • 8
  • 21
  • 2
    How are your examples a security issue? There is no SQL Injection possible. The security issue would be to use `$cond` (without converting it to an integer) to specify the minimum age in the query, and to check if `isset($_REQUEST['cond'])` is actually true (otherwise using `1 = 1`). – Arseni Mourzenko Nov 16 '11 at 20:19
4

Most of time developer use these type of query if he is developing a query builder type application or building some complex SQL query so along with the select statement string add a conditional clause Where 1=1, and in program no need to add any check for it.

2

The query finds all rows for which 1 equals 1 and value equals 'TOYOTA'. So in this case it's useless, but if you omit a WHERE statement, it can be a good idea to use WHERE 1=1 to remind you that you chose NOT to use a WHERE clause.

toon81
  • 868
  • 1
  • 5
  • 13
1

the use of this comes in complex queries when passing conditions dynamically,You can concatenate conditions using an " AND " string. Then, instead of counting the number of conditions you're passing in, you place a "WHERE 1=1" at the end of your stock SQL statement and throw on the concatenated conditions.

no need to use 1=1 you can use 0=0 2=2,3=3,5=5 25=25 ......

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

here also you will get the same result like 1=1 condition

because all these case is always true expression

1=1 is alias for true
Nighil
  • 4,099
  • 7
  • 30
  • 56
0

i did this when i need to apply the filters dynamically.
like, while coding i dunno how many filter user will apply (fld1 = val1 and fld2=val2 and ...)
so, to repeat the statement "and fld = val" i start with "1 = 1".
hence, i need not trim the first "and " in the statement.

BobbyGoks
  • 11
  • 2