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

I saw many people make use of a statement to question a table inside a MySQL database such as the following:

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

But exactly what does 1=1 mean here?

It's often when we develop SQL claims.

Whenever you add and value = "Toyota" it's not necessary to be worried about whether there's an ailment before or simply WHERE. The optimiser should neglected

No miracle, 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 need to possess a complicated group 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;

In the event that totally being built dynamically, original author most likely does not be thinking about a clear group of conditions, so finishes with something similar to this:

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

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


1=1 will be true, therefore the value="TOYOTA" bit may be the important one.

You receive this inside a couple of situations including:

Produced SQL: Its simpler to produce a produce a complex where statement without having to sort out if you are adding the very first condition or otherwise, so frequently a 1=1 is defined at the start, and all sorts of other condititions could be appended by having an And

Debugging: Sometimes the thing is people include a 1=1 towards the top of a where condition because it allows these to freely chop and alter the relaxation from the conditions when debugging a question. e.g.

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

It needs to be stated it is not particularly sound practice and should not occur normally being produced code. It might even not assist the optimisation from the query greatly.

In addition to the rest of the solutions, it is a simple way of SQL injection attacks. Should you give a OR where 1=1 statement with a SQL then it is going to return all of the results because of the natural truthiness from the expression.

Its just an always true expression. Many people utilize it being an work-around.

There is a static statement like:

select * from car_table where 1=1

To allow them to now add something towards the where clause with

and someother filter