How does MySQL implement decode (…) in Oracle (...) Functions

Source: Internet
Author: User

During data capturing, You must select and judge the SQL statement. Refer to the original script to find one of them.Decode (value, if1, then1, if2, then2,
If3, then3 ,... else) the function can meet this requirement. An error is reported after it is used. I found that this function can be used in Oracle Data and MySQL database cannot be used. How can I implement MySQL, there is a lot of information on the Internet. You can find the problem and solution on the Internet. The following describes how to use it.

Example: In Oracle (decode ):
Select sum (STI. find_questions) as findtotal, sum (STI. update_questions) as updatetotal, round (100 * (decode (sum (STI. update_questions), null, 0, sum (STI. update_questions)/(decode (sum (STI. update_questions), null, 0, sum (STI. update_questions) as zgl_0 from yujing. special_task_inspect STI where task_id =? And STI. inspect_time >=? And STI. inspect_time <=?

Corresponding to MySQL (select case when *** then *** else *** end ):

Select sum (STI. find_questions) as findtotal, sum (STI. update_questions) as updatetotal, round (100 * (select case when sum (STI. update_questions) = NULL then 0 else sum (STI. update_questions) End)/(select case when sum (STI. find_questions) = NULL then 0 else sum (STI. find_questions) End) as zgl_0 from yujing. special_task_inspect STI where task_id =? And STI. inspect_time >=? And STI. inspect_time <=?

Conclusion: as long as the key points of the problem are found, it is easy to solve the problem.

In addition, you can go to order by in use decode .

Example: TableTable_subject, YesSubject_nameColumn. Sort by language, number, and external order. This makes it easy to use.DecodeThe requirement has been completed.

 
Select * From table_subject order bydecode (subject_name, '', 1, 'mat', 2, '', 3)

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.