Get the App
SLTechnology News&Howtos  ›  Database  › 

The usage of "where 1" in SQL

Shulou Source: shulou.com Published: 2022-05-31 23:51:47 09月12日 Update

The purpose of this article is to share with you about the use of "where 1" in SQL. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The use of where 1 / 1 in SQL

Explanation:

In fact, 1x1 is permanent, meaning unconditional, that is to say, it doesn't matter whether you have it or not in the SQL statement.

This 1q1 is often used when applications piece together where conditions according to different user choices.

For example, the web interface queries the user's information, and where defaults to 1: 1, so that even if the user does not select any conditions, the sql query will not make an error. If the user chooses a name, then where becomes where 1 name 1 and name = 'name entered by the user', and if other conditions are selected, just keep appending and statements to the where condition.

If you don't need 1: 1, every additional condition, you have to judge whether there is a where condition or not, and if not, write where. Write and statements as soon as you have them, so you can simplify the complexity of your application by using 1x1 at this time.

Example:

As in the following code, $where='1 is defined first, and then you don't have to determine whether $where exists or not.

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; / / usefulness $where =' 1room1'; $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) } Thank you for reading! This is the end of this article on the usage of "where 1 in SQL". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Conditions users choices names statements no content applications more usefulness programs articles applications queries different good complex practical continuous that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information MariaDB Shulou Tech Info vpn