MySQL Three mesh operation usage example

Source: Internet
Author: User

The web business needs to be sorted by actual amount of money, with three fields: price (commodity prices), pay (actual paid price), Num (quantity purchased). If the product has a discount is based on pay prices, that is, pay>0, if there is no discount is the price of the deal, pay=0. So the actual price we want is if pay>0 is pay*num, otherwise it is price*num. This is to use three mesh operation pay >0?pay:price.

So does MySQL have three-mesh operators?

When you can use case, this MySQL should support: Select A,case when B is null then ' xxxx ' else B-B from table_xxx where. and this.

SQL is also supported in the three-mesh operation, the syntax is:

case when condition THEN if conforming ELSE if not conforming to end

So the SQL statements in our business are:

The Select Case is pay>0 THEN pay*num ELSE price*num end as ' amount ' from table_name to amount;

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.