If you do not use the judgment operation to ensure that if any number is greater than 30, it is 30; otherwise, it is the original value (maximum limit on Field Values in SQL sorting), and 30 SQL statements

Source: Internet
Author: User

If you do not use the judgment operation to ensure that if any number is greater than 30, it is 30; otherwise, it is the original value (maximum limit on Field Values in SQL sorting), and 30 SQL statements

Scenario: in a database, you need to sort a specific result. When sorting, the results are sorted by different weights of several fields, but the maximum weight of one field is limited to 30.

Analysis: in SQL, is a> 30? 30: only basic operations (+-*/) and logical operations (& | xor) such as a are supported.

Answer:

(A-a % 30) & 1) * 30: When a is less than 30, a-a % 30 is 0, (0 & 1) * if the result of 30 is 0 and a is greater than or equal to 30, the final result is 30.

(A-a % 30) xor 1: When a is greater than or equal to 30, the value is 0. If a is less than 30, the result is 1.

(A-a % 30) xor 1) * (a % 30): When a is greater than or equal to 30, the value is 0. If a is less than 30, the result is.

Final: (a-a % 30) & 1) * 30 + (a-a % 30) xor 1) * (a % 30)

When a is less than 30, the partial operation before the plus sign is 0, the subsequent operation is a, and the sum is.

When a is greater than or equal to 30, the partial operation before the plus sign is 30, the subsequent operation is 0, and the sum is 30.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.