MySQL Must Know 10: Mathematical Calculation, String Processing and Conditional Judgment

The reading is compiled from "MySQL Must Know and Know" - Zhu Xiaofeng . For details, please log in to the purchase column on the official website of Geek Time .

math function

It is mainly used to process numerical data. There are mainly three types commonly used, namely the rounding function ROUND(), CEIL(), FLOOR(), the absolute value function ABS() and the remainder function MOD().

data preparation:

demo.transactiondetail

mysql> select transactionid, itemnumber, quantity, price, discount, salesvalue
    -> from demo.transactiondetails
    -> order by transactionid, itemnumber;
+---------------+------------+----------+-------+----------+------------+
| transactionid | itemnumber | quantity | price | discount | salesvalue |
+---------------+------------+----------+-------+----------+------------+
|             1 |          1 |        2 | 89.00 |     0.99 |     176.22 |
|             1 |          2 |        5 |  5.00 |     0.99 |      24.75 |
|             2 |          1 |        3 | 89.00 |     0.88 |     234.96 |
|             2 |          2 |        6 |  5.00 |     0.88 |      26.40 |
|             3 |          1 |        1 | 89.00 |     0.95 |      84.55 |
|             3 |          2 |       10 |  5.00 |     0.95 |      47.50 |
+---------------+------------+----------+-------+----------+------------+

demo.transacionhead

mysql> select transactionid, transactionno, cashierid, memberid, operatorid, transdate
    -> from demo.transactionhead;
+---------------+------------------+-----------+----------+------------+---------------------+
| transactionid | transactionno    | cashierid | memberid | operatorid | transdate           |
+---------------+------------------+-----------+----------+------------+---------------------+
|             1 | 0120201201000001 |         1 |        1 |          1 | 2020-12-01 00:00:00 |
|             2 | 0120201202000001 |         1 |        2 |          2 | 2020-12-02 00:00:00 |
|             3 | 0120201202000002 |         1 |     NULL |          1 | 2020-12-01 01:00:00 |
+---------------+------------------+-----------+----------+------------+---------------------+

demo.goodsmaster

mysql> select * from demo.goodsmaster;
+------------+---------+-----------+---------------+------+-----------+
| itemnumber | barcode | goodsname | specification | unit | saleprice |
+------------+---------+-----------+---------------+------+-----------+
|          1 | 0001    ||               ||     89.00 |
|          2 | 0002    ||               ||      5.00 |
+------------+---------+-----------+---------------+------+-----------+

demo.membermaster

mysql> select memberid, branchid, cardno, membername, address, phone, pid, memeberpoints, memberdeposit from demo.membermaster;
+----------+----------+----------+------------+---------+-------------+--------------------+---------------+---------------+
| memberid | branchid | cardno   | membername | address | phone       | pid                | memeberpoints | memberdeposit |
+----------+----------+----------+------------+---------+-------------+--------------------+---------------+---------------+
|        1 |        1 | 10000001 | 张三       | 天津    | 13698765432 | 475145197001012356 |          0.00 |          0.00 |
|        2 |        1 | 10000002 | 李四       | 上海    | 18758079161 | 123123199001012356 |          0.00 |          0.00 |
+----------+----------+----------+------------+---------+-------------+--------------------+---------------+---------------+

rounding function

  • Ceil(X) and ceiling(X): return the smallest INT integer greater than or equal to X
  • Floor(X): Returns the largest INT integer less than or equal to X
  • Rounding function round(X,D): X represents the number to be processed, D represents the number of decimal places reserved, the processing method is rounding, ROUND(X) represents 0 decimal places

Requirements : The rule of points is one point for one yuan, and no points for less than one yuan. Obviously, it is rounded down, so you can use the FLOOR() function.

Obtain relevant information about member consumption through associated query:

mysql> select c.membername as '会员',
    -> b.transactionno as '单号',
    -> b.transdate as '时间',
    -> d.goodsname as '商品名称',
    -> a.salesvalue as '交易金额'
    -> from demo.transactiondetails a
    -> join demo.transactionhead b on (b.transactionid = a.transactionid)
    -> join demo.membermaster c on (c.memberid = b.memberid)
    -> join demo.goodsmaster d on (d.itemnumber = a.itemnumber);
+------+------------------+---------------------+----------+----------+
| 会员 | 单号             | 时间                | 商品名称 | 交易金额 |
+------+------------------+---------------------+----------+----------+
| 张三 | 0120201201000001 | 2020-12-01 00:00:00 ||   176.22 |
| 张三 | 0120201201000001 | 2020-12-01 00:00:00 ||    24.75 |
| 李四 | 0120201202000001 | 2020-12-02 00:00:00 ||   234.96 |
| 李四 | 0120201202000001 | 2020-12-02 00:00:00 ||    26.40 |
+------+------------------+---------------------+----------+----------+

Use floor(a.salesvalue), round down the sales amount to get the member points value

select c.membername as '会员',
b.transactionno as '单号',
b.transdate as '时间',
d.goodsname as '商品名称',
floor(a.salesvalue) as '积分'
from demo.transactiondetails a
join demo.transactionhead b on (b.transactionid = a.transactionid)
join demo.membermaster c on (c.memberid = b.memberid)
join demo.goodsmaster d on (d.itemnumber = a.itemnumber);

+------+------------------+---------------------+----------+------+
| 会员 | 单号             | 时间                | 商品名称 | 积分 |
+------+------------------+---------------------+----------+------+
| 张三 | 0120201201000001 | 2020-12-01 00:00:00 ||  176 |
| 张三 | 0120201201000001 | 2020-12-01 00:00:00 ||   24 |
| 李四 | 0120201202000001 | 2020-12-02 00:00:00 ||  234 |
| 李四 | 0120201202000001 | 2020-12-02 00:00:00 ||   26 |
+------+------------------+---------------------+----------+------+

or:

mysql> select 1.5 - mod(1.5, 1);
+-------------------+
| 1.5 - mod(1.5, 1) |
+-------------------+
|               1.0 |
+-------------------+

mysql> select 1.1 - mod(1.1, 1);
+-------------------+
| 1.1 - mod(1.1, 1) |
+-------------------+
|               1.0 |
+-------------------+

Requirement : When receiving cash, the amount receivable can be set to which digit to round to. For example, you can set rounding to yuan, to angle, or to cent.

mysql> select round(salesvalue, 2)
    -> from demo.transactiondetails where transactionid=1 and itemnumber=1;
+----------------------+
| round(salesvalue, 2) |
+----------------------+
|               176.22 |
+----------------------+

mysql> select round(salesvalue, 1)
    -> from demo.transactiondetails where transactionid=1 and itemnumber=1;
+----------------------+
| round(salesvalue, 1) |
+----------------------+
|                176.2 |
+----------------------+

mysql> select round(salesvalue)
    -> from demo.transactiondetails where transactionid=1 and itemnumber=1;
+-------------------+
| round(salesvalue) |
+-------------------+
|               176 |
+-------------------+

string functions

There are 4 commonly used string functions.

  • concat(s1,s2,…): means to concatenate the strings s1, s2… to form a string
  • cast(expression as char): Indicates converting the value of the expression into a string
  • char_length (string): Indicates the length of the obtained string
  • space(n): means to get a string consisting of n spaces

Requirement : After the transaction is completed, the system must issue a receipt. When printing receipts, there are many requirements for the format. For example, a small ticket paper with a width of 57mm can hold about 32 characters, that is, 16 Chinese characters. The user requires 2 lines for a stream, the first line is product information, and the second line should include four types of information: quantity, price, discount and amount. So, how can we clearly print this information on the receipt, and print it neatly and beautifully?

  1. Print the product information on the first line. Product information includes: product name and product specification, and the product specification should be included in brackets. In this way, the product name and product specification must be spliced ​​together to form a character string.

    mysql> select concat(goodsname, '(', specification, ')') as 商品信息
        -> from demo.goodsmaster where itemnumber=1;
    +----------+
    | 商品信息 |
    +----------+
    |(16) |
    +----------+
    
  2. How to print the second line. The second line includes quantity, price, discount and amount, and there are 4 kinds of information in total.

    convert to string

    mysql> select cast(quantity as char)  -- 把数量转换成字符串,把decimal类型转换成字符串
        -> from demo.transactiondetails
        -> where transactionid=1 and itemnumber=1;
    +------------------------+
    | cast(quantity as char) |
    +------------------------+
    | 2                      |
    +------------------------+
    

    Calculate the length of a string

    mysql> select char_length(cast(quantity as char)) as 长度
        -> from demo.transactiondetails where transactionid=1 and itemnumber=1;
    +------+
    | 长度 |
    +------+
    |    1 |
    +------+
    

    Space padding 7-digit length

    mysql> select concat(cast(quantity as char), space(7-char_length(cast(quantity as char)))) as 数量
        -> from demo.transactiondetails where transactionid=1 and itemnumber=1;
    +---------+
    | 数量    |
    +---------+
    | 2       |
    +---------+
    

String functions: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html


condition judgment function

The main function of the condition judgment function is to return different values ​​according to specific conditions. There are two commonly used ones:

  • ifnull(V1, V2): Indicates that if the value of V1 is not null, return V1, otherwise return V2
  • if(表达式, V1, V2): If the expression is true (TRUE), return V1, otherwise return V2
mysql> select goodsname, specification, concat(goodsname, '(', ifnull(specification, ''), ')') as 拼接
    -> from demo.goodsmaster;
+-----------+---------------+----------+
| goodsname | specification | 拼接     |
+-----------+---------------+----------+
|| 16|(16) |
|| NULL          |()     |
+-----------+---------------+----------+
mysql> select goodsname, specification, if(isnull(specification), goodsname, concat(goodsname, '(', ifnull(specification, ''), ')')) as 拼接
    -> from demo.goodsmaster;
+-----------+---------------+----------+
| goodsname | specification | 拼接     |
+-----------+---------------+----------+
|| 16|(16) |
|| NULL          ||  -- 去掉多余的()
+-----------+---------------+----------+

summary

function Function
math function floor(x): Get the largest integer less than or equal to x
ceil(x): Get the smallest integer greater than or equal to x
round(x, d): By rounding, obtain the value closest to x, retaining d decimal places
abs(): get the absolute value of x
mod(x, y): Get the remainder after x is divided by y
concat(s1,s2,…): Concatenate strings s1, s2,…
char_length(s): Get the number of characters in the string s, one Chinese character counts as one character
string functions space(n): Get a string consisting of n spaces
substr() and mid(): get the substring in the string
trim(): delete the substring at both ends of the beginning of the string
ltrim(): Remove spaces at the beginning of the string
rtrim(): remove the spaces at the end of the string
condition judgment function ifnull(v1, v2): If v1 is null, return v2, otherwise return v1
if(expression, v1, v2): If the expression is true, return v1, otherwise return v2

The function also has some pitfalls, such as round(x) rounding x is not necessarily greater than the original x:

mysql> select round(-1.5);
+-------------+
| round(-1.5) |
+-------------+
|          -2 |
+-------------+

mysql> select round(-1.5, 0);
+----------------+
| round(-1.5, 0) |
+----------------+
|             -2 |
+----------------+

mysql> select round(-1.5, 1);
+----------------+
| round(-1.5, 1) |
+----------------+
|           -1.5 |
+----------------+

Guess you like

Origin blog.csdn.net/qq_31362767/article/details/123602671