In SQL, It's easier to make a generate a posh wherever statement if you do not need to compute if you are adding the primary condition or not, therefore usually a 1=1 is placed in the beginning, and every one alternative condition may be appended with an AND Syntax.
Here, 1=1 will always be true. While debugging sometimes you observe people use '1=1' at the top of a where condition as it allows them to freely change the rest of the conditions when they are debugging the query.
When you are adding and value=" Toyota" then you no need to worry about whether there is a condition before or just WHERE.
Have a look at the below example.
QUERY:
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 will have to have a complicated set of logic like this:
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;