mysql will certainly must know - using a data processing function

function

Like most other computer languages, SQL support the use of functions to process data. Function
is generally performed on the data, and converts it to provide a convenient data processing.
String used to remove the trailing space in the previous chapter RTrim () is an example of a function

Function without SQL portability can be run on multiple systems called code
to be portable (portable). Relatively speaking, the majority of SQL statements are portable,
while there are differences between SQL implementations, these differences are usually not so intractable. The letter
portability number is not strong. Almost every major DBMS implementation of its support
him achieve the function is not supported, and sometimes the difference is still very great.
For code portability, many SQL programmers deprecated achieve special functions
can. Although this is very good, but not always conducive to the application's performance. As
if not to use these functions, write some application code will be very difficult. Lee must
use other methods to achieve DBMS very efficiently done.
If you decide to use the function, you should ensure good code comments, so that after you (or
others) can know exactly the meaning of written SQL code

Use function

Most SQL implementations support the following types of functions.

  • For processing a text string (e.g., deletion or fill value, the conversion value of upper or lower case) of the text
    of this function.
  • For performing arithmetic operations on numerical data (e.g., returns the absolute value, for algebra)
    valued functions.
  • Date and time values for processing and extracting a particular component from these values (e.g., return
    date and time function of a difference between two dates, check the validity of the date, etc.).
  • DBMS being used to return specific information (such as user login information is returned, check the version
    details) of system function

Text handling functions

Using the Upper () function

As seen, the Upper () to convert the text to upper case, and therefore each of the present example for
vendors are listed twice, the first time vendors values stored in the table, as the second
column vend_name_upcase converted to uppercase


Table 11-1 SOUNDEX require further explanation. SOUNDEX is any text to a
present algorithm described string into its phonetic representations of alphanumeric mode. SOUNDEX consider a similar
pronunciation of characters and syllables that can compare to pronounce string comparison instead of letters. Although
SOUNDEX not SQL concept, but MySQL (just as most DBMS) provides for
support of SOUNDEX.

An example is given below using the function Soundex (). customers table has a care
customer Coyote Inc., its contact named Y.Lee. But if this is a mistake, contact name and reality this
occasion should be Y.Lie, how do? Obviously, the correct contact search by name does not return data, such as
under shown

Now try to use Soundex () function to search, it matches all the pronunciation is similar to
Y.Lie contact name

In this example, WHERE clause using the Soundex () function to convert cust_
Contact column values and the search string to their SOUNDEX values. Because Y.Lee and
similar pronunciation Y.Lie, so their SOUNDEX values match, and therefore the WHERE clause properly filtered
out of the required data

The date and time handling functions

期和时间采用相应的数据类型和特殊的格式存储,以便能快速和
有效地排序或过滤,并且节省物理存储空间。
一般,应用程序不使用用来存储日期和时间的格式,因此日期和时
间函数总是被用来读取、统计和处理这些值。由于这个原因,日期和时
间函数在MySQL语言中具有重要的作用

用日期进行过滤需要注意一些别的问题和使用特殊的
MySQL函数。
首先需要注意的是MySQL使用的日期格式。无论你什么时候指定一
个日期,不管是插入或更新表值还是用 WHERE 子句进行过滤,日期必须为
格式yyyy-mm-dd。因此,2005年9月1日,给出为2005-09-01。虽然其他的
日期格式可能也行,但这是首选的日期格式,因为它排除了多义性(如,
04/05/06是2006年5月4日或2006年4月5日或2004年5月6日或……)

应该总是使用4位数字的年份 支持2位数字的年份,MySQL
处理00-69为2000-2069,处理70-99为1970-1999。虽然它们可
能是打算要的年份,但使用完整的4位数字年份更可靠,因为
MySQL不必做出任何假定。

但是,使用 WHERE order_date = '2005-09-01' 可靠吗? order_
date 的数据类型为 datetime 。这种类型存储日期及时间值。样例表中
的值全都具有时间值 00:00:00 ,但实际中很可能并不总是这样。如果
用当前日期和时间存储订单日期(因此你不仅知道订单日期,还知道
下订单当天的时间),怎么办?比如,存储的 order_date 值为
2005-09-01 11:30:05 ,则 WHERE order_date = '2005-09-01' 失败。
即使给出具有该日期的一行,也不会把它检索出来,因为 WHERE 匹配失
败。
解决办法是指示MySQL仅将给出的日期与列中的日期部分进行比
较,而不是将给出的日期与整个列值进行比较。为此,必须使用 Date()
函数。 Date(order_date) 指示MySQL仅提取列的日期部分,更可靠的
SELECT 语句为:

如果要的是日期,请使用 Date() 如果你想要的仅是日期,
则使用 Date() 是一个良好的习惯,即使你知道相应的列只包
含日期也是如此。这样,如果由于某种原因表中以后有日期和
时间值,你的SQL代码也不用改变。当然,也存在一个 Time()
函数,在你只想要时间时应该使用它。
Date() 和 Time() 都是在MySQL 4.1.1中第一次引入的。

如果你想检索出2005年9月下的
所有订单,怎么办?简单的相等测试不行,因为它也要匹配月份中的天
数。有几种解决办法,其中之一如下所示

其中, BETWEEN 操作符用来把 2005-09-01 和 2005-09-30 定义为
一个要匹配的日期范围。
还有另外一种办法(一种不需要记住每个月中有多少天或不需要操
心闰年2月的办法)

Year() 是一个从日期(或日期时间)中返回年份的函数。类似,
Month() 从日期中返回月份。因此, WHERE Year(order_date)
= 2005 AND Month(order_date) = 9 检索出 order_date 为2005年9月的
所有行

MySQL的版本差异 MySQL 4.1.1中增加了许多日期和时间
函数。如果你使用的是更早的MySQL版本,应该查阅具体的
文档以确定可以使用哪些函数

数值处理函数

数值处理函数仅处理数值数据。这些函数一般主要用于代数、三角
或几何运算,因此没有串或日期 — 时间处理函数的使用那么频繁。
具有讽刺意味的是,在主要DBMS的函数中,数值函数是最一致最统
一的函数。表11-3列出一些常用的数值处理函数

本章介绍了如何使用SQL的数据处理函数,并着重介绍了日期处理函
数。

Guess you like

Origin www.cnblogs.com/ygjzs/p/12229961.html
Recommended