where 1=1 in sql

where 1=1 is the conditional logic judgment expression of the sql statement, since 1=1 holds true and is always true, 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 return the logical value "True", but 1 =1 has less computational overhead, so it is the most commonly used. 

The following example will help to understand the concept:

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

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

description, example 1) is actually equivalent to not adding any filter conditions, some superfluous, where 1=1 The actual meaning of where 1<>1 is not as useful as where 1<>1. When we only need to obtain the field (structure) information of the table and do not need to care about the actual saved records, the writing method of example 2) will be very desirable, because the system Only structural information will be read, and specific table records will not be read into memory, which undoubtedly saves system overhead.

おすすめ

転載: blog.csdn.net/sunzongpeng/article/details/79648969