What does where 1=1 mean in the sql statement

where 1=1 is the conditional logic judgment expression of the SQL statement. Since 1=1 is established, it is always true, and the expression 1=1 will always return "true".
The actual purpose of this writing method is to obtain the logical value "True". In fact, writing methods such as 2=2, 1+2=3, '中'='中', etc. can be used.
To return the logical value "True", but the operation cost of 1=1 is smaller, so it is the most commonly used.

The following examples will help to understand the concepts:

1) select * from t1 where 1=1;
-- actually equivalent to select * from t1 where true;
-- statement will return all record rows in t1

2) select * from t1 where 1<>1;
-- actually equivalent to select * from t1 where false;
-- statement will return empty recordset

Explanation, example 1) is actually equivalent to not adding any filtering conditions, some superfluous,
The actual meaning of where 1=1 is not as useful as where 1<>1, when we only need to get the words of the table
When the segment (structural) information is not required to be concerned about the actual saved records, the writing method of example 2) will be very desirable,
Because the system only reads the structure information, and does not read specific table records into memory, this undoubtedly saves system overhead.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292887&siteId=291194637