<<連載>><<MariaDB cr

來源:互聯網
上載者:User

第十一章:使用資料處理函數

Like almost any other computer language, SQL supports the use of functions
to manipulate data. Functions are operations usually performed on data, usually
to facilitate conversion and manipulation.
像大部分其他電腦語言一樣,sql支援函數去處理資料,函數就是有利於資料轉換和處理的一些操作。

1.常見的文本處理函數
Function Description
Left()Returns characters from left of string
Length()Returns the length of a string
Locate()Finds a substring within a string
Lower()Converts string to lowercase
LTrim()Trims white space from left of string
Right()Returns characters from right of string
RTrim()Trims white space from right of string
Soundex()Returns a string’s SOUNDEX value
SubString()Returns characters from within a string
Upper()Converts string to uppercase


2.常見的時間處理函數
AddDate() Add to a date (days, weeks, and so on)
AddTime() Add to a time (hours, minutes, and so on)
CurDate() Returns the current date
CurTime() Returns the current time
Date() Returns the date portion of a date time
DateDiff()Calculates the difference between two dates
Date_Add()Highly flexible date arithmetic function
Date_Format()Returns a formatted date or time strin
Day() Returns the day portion of a date
DayOfWeek()Returns the day of week for a date
Hour() Returns the hour portion of a time
Minute() Returns the minute portion of a time
Month() Returns the month portion of a date
Now() Returns the current date and time
Second() Returns the second portion of a time
Time() Returns the time portion of a date tim
Year() Returns the year portion of a date

3.常見的數學運算函數
Abs() Returns a number’s absolute value
Cos() Returns the trigonometric cosine of a specified angle
Exp() Returns the exponential value of a specific number
Mod() Returns the remainder of a division operation
Pi() Returns the value of pi
Rand() Returns a random number
Sin() Returns the trigonometric sine of a specified angle
Sqrt() Returns the square root of a specified number
Tan() Returns the trigonometric tangent of a specified angle


第十二章:統計數值

It is often necessary to summarize data without actually retrieving it all, and
MariaDB provides special functions for this purpose. Using these functions,
MariaDB queries are often used to retrieve data for analysis and reporting pur
poses.

通常情況下,我們根本沒必要去接收數值,而是僅僅想統計那些數值,MariaDB
提供了這樣的函數,讓我們可以分析和統計數值

常見的統計函數
AVG() Returns a column’s average value平均值)
COUNT() Returns the number of rows in a column總數)
MAX() Returns a column’s highest value最大值)
MIN() Returns a column’s lowest value最小值)
SUM() Returns the sum of a column’s values總和)

這些函數還可以搭配DISTINCT使用,這裡不一一贅述。

Aggregate functions are used to summarize data. MariaDB supports a range of
aggregate functions, all of which can be used in multiple ways to return just the
results you need. These functions are designed to be highly efficient, and they
usually return results far more quickly than you could calculate them yourself
within your own client application.

統計函數在mariadb中很有效率,通常要比把資料取回資料自己運算的方式要來的快



第十三章:資料分組

In this chapter, you learn how to group data so you can summarize subsets of table
contents. This involves two new SELECTstatement clauses: the GROUP BYclause
and the HAVINGclause.
這一章教你怎樣把整體的資料分解成子組來統計,這將引入兩個子句:GROUP BY和HAVING

You’ve already seen the WHEREclause in action (introduced back in Chapter 6,
“Filtering Data.”) But WHEREdoes not work here because WHEREfilters specific
rows, not groups. As a matter of fact, WHEREhas no idea what a group is.
So what do you use instead of WHERE? MariaDB provides yet another clause
for this purpose: the HAVINGclause. HAVINGis similar to WHERE. In fact, all types
ofWHEREclauses you learned about thus far can also be used with HAVING. The
only difference is that WHEREfilters rows and HAVINGfilters groups.
同學們都在前幾章見識了where的使用方法,但問題是,where只能篩選行,不能篩選分組,於是
MariaDB引入了新的子句“HAVING”來專門處理GROUP的篩選問題。

SELECT cust_id, COUNT(*) AS orders
FROM orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;


More often,you will find that data grouped using GROUP BYwill indeed be output
in group order. But that is not always the case, and it is not actually required by
the SQL specifications. Furthermore, you might actually want it sorted differ
ently than it is grouped. Just because you group data one way (to obtain group
specific aggregate values) does not mean that you want the output sorted that
same way. You should always provide an explicit ORDER BYclause as well, even
if it is identical to the GROUP BYclause。
經常性地,你會探索資料確實是按照分組的方式輸出的,但每個分組裡的資料的次序卻不是
你想要的,所以你需要顯式地提供order by子句,即使它跟group by是相同的。

相關文章

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.