读书人

为何查询语句中经常会有quot;1=1quot

发布时间: 2012-07-19 16:02:19 作者: rapoo

为什么查询语句中经常会有"1=1"

目的是动态查询,查询语句先加上WHERE 1=1,然后后面的条件不论是有一个或者多个都可以直接写为AND <condition>.

?

dim sqlstmt as new StringBuildersqlstmt.add("SELECT * FROM Products")sqlstmt.add(" WHERE 1=1") ''// From now on you don't have to worry if you must ''// append AND or WHERE because you know the WHERE is thereIf ProductCategoryID <> 0 then  sqlstmt.AppendFormat(" AND ProductCategoryID = {0}", trim(ProductCategoryID))end ifIf MinimunPrice > 0 then  sqlstmt.AppendFormat(" AND Price >= {0}", trim(MinimunPrice))end if
?

?

?

http://stackoverflow.com/questions/242822/why-would-someone-use-where-1-1-and-conditions-in-a-sql-clause

读书人网 >其他数据库

热点推荐