SQL functions: DATE, FLOOR, UNION, DATE_SUB, IFNULL and mark CDATA

 <select id="getNeKpiTrend" resultMap="monitorNumVo">
        SELECT
        w.orderDate as kpi_time,ifnull(res.orderSum,0) as total
        FROM
        (
        SELECT DATE_SUB(CURDATE(), INTERVAL m.s-1 DAY) as orderDate from (
        SELECT 1 as s UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
        UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14
        UNION ALL SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22
        UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL SELECT 30
        ) m
        ) w
        LEFT JOIN
        (
        SELECT COUNT(*) as orderCount,FLOOR(sum(kpi_value)) as orderSum ,date(kpi_time) as orderDate
        from monitor_statistics_history WHERE kpi_id = #{kpiId} and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <![CDATA[ < ]]> date(kpi_time) AND date(kpi_time) <![CDATA[ <= ]]>  DATE(CURDATE())
        GROUP BY orderDate
        )res ON w.orderDate = res.orderDate order by w.orderDate ASC

    </select>

This is a recent trend of indicators inquiry within 30 days of the SQL statement, which uses the following SQL functions:

1. DATE () function

Syntax : DATE(date)
Usage : DATE () function returns the date or date / time expression of the date part.

Use the date function in the above SQL statement to query orderDate
Here Insert Picture Description
understand : DATE () function is the main role of the date of the data look-up table, because we used the recording time field usually with minutes and seconds, when the packet to date when the standards, we need to use the DATE () function.

W3school link

2. FLOOR () function

Syntax : FLOOR(x)
Usage : the FLOOR (x) function returns the greatest integer less than x.
Appreciated : and FLOOR (x) similar to the rounding function has ROUND (x), except that ROUND (x) is rounded in rounding.
Take the difference between the two integral function

3. UNION ALL 和 UNION

UNION operation result set operator for combining two or more of the SELECT statement.
By default, UNION operator to select a different value. If you allow duplicate values, use UNION ALL.

Extensive use UNION ALL function in the above SQL statement,

SELECT DATE_SUB(CURDATE(), INTERVAL m.s-1 DAY) as orderDate from (
        SELECT 1 as s UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7
        UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14
        UNION ALL SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22
        UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29 UNION ALL SELECT 30
        ) m

This SQL query contains the role of today's date and 30 days before today.
Note : Internal UNION SELECT statement must have the same number of columns. The columns must also have similar data types. Meanwhile, the order of columns in each SELECT statement must be the same.

W3school link

4. DATE_SUB () function

Syntax : DATE_SUB(date,INTERVAL expr type)
Usage : date parameter is valid date expression. expr parameter is the time interval you want to add. expr plurality of parameters can be selected.

W3school link

5. IFNULL () function

Syntax : IFNULL(expression, alt_value)
Usage : if the expression of the first parameter expression is NULL, the backup second argument is returned.

W3school link

In particular, SQL statements, there are a <! [CDATA []]>

Usage : this marker is included in the plain text is represented as, for example <[CDATA [<]]> represents text "<" <[CDATA [] ]!>!.

This tag is actually played a role in the escape, because the xml document, "<", ">", "&" and other characters are not directly deposited, otherwise it will error when the xml grammar check, if you want to xml the use of these symbols, the entity must be escaped. For shorter characters we can use the corresponding escape characters to solve this problem, but for a string longer need to be escaped and was not very clear. So we must use this tag.
We understood that : the term refers to text data CDATA (Unparsed Character Data) should not be parsed by the XML parser.
Reference article link
W3school explanation

Published 19 original articles · won praise 4 · Views 1567

Guess you like

Origin blog.csdn.net/DATANGguanjunhou/article/details/104248718