SQL 语句中 Where 条件后 写上1=1 是什么意思
导读:这篇文章主要介绍了title,小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望大家通过这篇文章可以有所收获。 SQL中where 1=1的用处解释:其实,1=1 是永恒成立的,意思无条件的,也就是说在SQL语句...
这篇文章主要介绍了title,小编觉得挺不错的,现在分享给大家,也给大家做个参考,希望大家通过这篇文章可以有所收获。
SQL中where 1=1的用处
解释:
其实,1=1 是永恒成立的,意思无条件的,也就是说在SQL语句中有没有这个1=1都可以。
这个1=1常用于应用程序根据用户选择项的不同拼凑where条件时用的。
如:web界面查询用户的信息,where默认为1=1,这样用户即使不选择任何条件,sql查询也不会出错。如果用户选择了姓名,那么where变成了where 1=1 and 姓名=‘用户输入的姓名’,如果还选择了其他的条件,就不断在where 条件后追加 and语句就行了。
如果不用1=1的话,每加一个条件,都要判断前面有没有where 条件,如果没有就写where …,有就写and语句,因此此时用1=1可以简化了应用程序的复杂度。
例:
如下面代码首先定义$where= ‘1=1’,后面就可以不用去判断是否存在$where
public function listAction() { $get = $this-> getQuery(); $statementBalanceDetailModel = M('Ticket\StatementBalanceDetail'); $page = isset($get['page']) ? intval($get['page']) : 1; $pageSize = isset($get['page_size']) ? intval($get['page_size']) : 10; //用处 $where = ' 1=1 '; $binds = array(); if (isset($get['id']) & & $get['id'] != '') { $where .= ' and id = :id'; $binds['id'] = trim($get['id']); } if (isset($get['shop_name']) & & $get['shop_name'] != '') { $where .= ' and shop_name = :shop_name'; $binds['shop_name'] = trim($get['shop_name']); } if (isset($get['statement_sn']) & & $get['statement_sn'] != '') { $where .= ' and statement_sn = :statement_sn'; $binds['statement_sn'] = trim($get['statement_sn']); } $where .= ' order by id desc'; $result = $statementBalanceDetailModel-> paginate($where, $pageSize, $page, $fields = array(), $binds); $sceneryList = $result['data']; $total = $result['total_result']; $pager = Paginate::web($total, $page, $pageSize); $data = array( 'pager' => $pager, 'sceneryList' => $sceneryList, ); $this-> getView()-> assign($data); }
以上就是关于“SQL 语句中 Where 条件后 写上1=1 是什么意思”的介绍了,感谢各位的阅读,希望文本对大家有所帮助。如果想要了解更多知识,欢迎关注网络,小编每天都会为大家更新不同的知识。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: SQL 语句中 Where 条件后 写上1=1 是什么意思
本文地址: https://pptw.com/jishu/650421.html