MySQL CASE expression

Source: Internet
Author: User

I. BACKGROUND

Resc table preserves the system's first-level, two-level functional modules, while preserving the function of the two-level function module pages, the PID defines the parent-child relationship.

Want to through the Resc table, sorting out a, level, level two function module correspondence, similar to this result.


Second, the realization method

Select Case when pid=1 then ' user Management ' when pid=4 then ' Agent Management ' when pid=8 then ' media Management ' when pid=21 then ' report Management ' when pid=26 then ' content tube Pid=30 then ' performance Management ' when pid=34 then ' financial Management ' END as menu, ' name ', PID from Rescwhere pid in (select id from Resc where pi d=0) ORDER by PID

Execution Result:

The results are pasted into Excel, and the merged cells are combined to get the expected results, which can be tracked in Excel.


Third, the theory

case expressions in SQL, you can implement If-then-else logic.

There are two types of case expressions:

    • Simple case expression, using an expression to determine the return value;

    • Searches the case expression, using conditions to determine the return value.


    1. Using simple case expressions

    • Grammar:

Case Search_expression

When Expression1 then RESULT1

When Expression2 then RESULT2

...

When Expressionn then RESULTN

else Default_result

End


    • Keyword Description:

      • Search_expression, the expression to be evaluated, a field of the data table;

      • Expressionx, is the expression that compares with the search_expression;

      • RESULT1, which is the value returned when the expression is met

      • Default_result, which is the default return value when an expression cannot be matched.



2. Search for Case expressions

    • Grammar:

Case

When Condition1 then RESULT1

When Condition2 then RESULT2

...

When Conditionn then RESULTN

else Default_result

End

    • Keyword Description:

      • Condition, is the expression to be evaluated;

      • result, which is the return value, Condition1 is true, returns RESULT1, and so on.

      • Default_result, which is the default return value when an expression cannot be matched.



MySQL CASE expression

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.