Home > Database > SQL > What is the use of 'where 1=1' in SQL?

What is the use of 'where 1=1' in SQL?

青灯夜游
Release: 2020-07-07 15:59:46
forward
8924 people have browsed it

What is the use of 'where 1=1' in SQL?

The use of where 1=1 in SQL

Explanation:

In fact, 1=1 is eternally true and means unconditional. That is to say, it does not matter whether there is 1=1 in the SQL statement.

This 1=1 is often used when applications piece together where conditions based on different user selections.

For example: the web interface queries user information, where defaults to 1=1, so that even if the user does not select any conditions, the SQL query will not go wrong. If the user selects a name, then where becomes where 1=1 and name=‘the name entered by the user’. If other conditions are selected, just keep appending and statements after the where condition.

If you don’t use 1=1, every time you add a condition, you have to judge whether there is a where condition in front of it. If not, write where..., if there is, write an and statement, so using 1=1 at this time can simplify it. Application complexity.

Example:

If the following code first defines $where= '1=1', then there is no need to judge whether $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);
    }
Copy after login
## exists. # This article is reproduced from: https://blog.csdn.net/longgeaisisi/article/details/100599010

Recommended learning:

mysql tutorial

The above is the detailed content of What is the use of 'where 1=1' in SQL?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template