If you do not use a judgment operation to ensure that any number is greater than 30 or 30, otherwise the original value (the maximum limit on the field value when sorting in SQL)

Source: Internet
Author: User

Scenario: In a database, you need to sort a result by a number of fields, sorted by a different weight, but the maximum weight of one field is limited to 30.

Analysis: There are no a>30?30:a operations in SQL, only basic operations (+-*/), logical operations (&& | | XOR) and so on.

Answer:

((a-a%30) &&1) *30:a less than 30 o'clock, a-a%30 is 0, (0&&1) *30 result is 0,a greater than or equal to 30 o'clock, the final result is 30

(a-a%30) XOR 1: When a is greater than or equal to 30 o'clock, the value is 0, less than 30 o'clock, and the result is 1

((A-A%30) XOR 1) * (A%30): When a is greater than or equal to 30 o'clock, the value is 0, less than 30 o'clock, the result is a

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

When a is less than 30 o'clock, a portion of the operation before the + number is 0, and the subsequent operation gets a, adding a

When a is greater than or equal to 30 o'clock, a part of the operation before the + number gets 30, and then the operation gets 0, and the sum is 30.

If you do not use a judgment operation to ensure that any number is greater than 30 or 30, otherwise the original value (the maximum limit on the field value when sorting in SQL)

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.