<<连载>><<MariaDB crash Course>>中文笔记(第六)

第九章:使用正则表达式

       all regular expressions do is match text, comparing a pattern (the regular expression) with a string of text. MariaDB provides rudimentary support for regular expressions with WHERE clauses, allowing you to specify regular expressions that are used to filter data retrieved using SELECT.

       正则表达式所做的,无非是匹配字符串,MariaDB提供了有限的正则表达式支持,一般用在select和where语句中.


        LIKE matches an entire column. If the text to be matched existed

       in the middle of a column value, LIKE would not find it and the row would not be returned (unless wildcard characters were used). REGEXP, on the other hand, looks for matches within column values, and so if the text to be matched existed in the middle of a column value, REGEXPwould find it and the row would be returned. This is an important distinction.

       在没有通配符的情况下,LIKE匹配一整行,如果字符串在字符串内部出现的话,like将找不到它,并且不会返回它,正则表达式则不同;这就是LIKE跟REGEXP最大的区别.


       Matches Are Not Case-Sensitive Regular expression matching in MariaDB is not case-sensitive (either case will be matched). To force case-sensitivity,you can use the BINARY keyword.

       正则表达式在MariaDB里是不区分大小写的,如果要区分,必须加BINARY限定词.


       To search for one of two strings (either one or the other), use '|'

       查找1个或2个字符串,使用'|' 符号


       SELECT prod_name

       FROM products

       WHERE prod_name REGEXP '1000|2000'

       ORDER BY prod_name;

       '|'将会起到逻辑运算中'OR'的作用


       .matches any single character. But what if you wanted to match only specific characters? You can do this by specifying a set of characters enclosed within '[',']'

       点号匹配单独的字符,如果你想匹配特定的字符呢?你可以用[]括起来一系列特定的字符来达到目的.

       SELECT prod_name

       FROM products

       WHERE prod_name REGEXP '[123] Ton'

       ORDER BY prod_name;



       whereas [123]matches characters 1, 2, or 3, [^123]matches anything

but those characters .

       [123]匹配1,2,3[^123]则匹配除了1,2,3其他任意字符


       Sets can be used to define one or more characters to be matched. For example, the following matches digits 0 through 9:[0123456789]

       To simplify this type of set, - can be used to define a range

       集合可以用来定义多个可以匹配的字符,举例来说0到9:[123456789]

       但这样太麻烦了,于是使用[0-9]来表示范围


       To match special characters they must be preceded by \\. So, \\-means find –and \\.means find .

       如何来表示[ ],'.','|'呢?用双back-slash "\\"来转义.


       SELECT vend_name

       FROM vendors

       WHERE vend_name REGEXP '\\.'

       ORDER BY vend_name;


       更多的特殊字符:

           \\f 表示 Form Feed; \\n表示换行符;\\t表示tab;\\v表示垂直tab

           \\\ 表示'\'


            Most regular expression implementations use a single backslash to escape special characters to be able to use them as literals. MariaDB, however, requires two backslashes (MariaDB itself interprets one, and the regular expression library interprets the other.

            大多数正则表达式调用,使用一个backslash来转义,但MariaDB使用两个,1个用来自己识别,里一个让正则表达式库识别.


            There are matches that you’ll find yourself using frequently—digits, or all alphabetical characters, or all alphanumerical characters, and so on. To make working with these easier, you may use predefined character sets known as character classes.

            有这样一类匹配:你总是会用到,比如[0-9],[a-z],[A-Z],为了更加方便的使用它们,于是就有了"预定义字符集"


            一些"预定义字符集":

            [:alnum:] 任意字母或数字[a-zA-Z0-9]

            [:alpha:] 任意字母[a-zA-Z]

            [:blank:]  空格或tab

            [:cntrl:] ASCII控制字符(ASCII 0 through 31 and 127)

            [:digit:] 任意数字[0-9]

            [:graph:] 跟[:print:]相同,但会打印空白

            [:lower:] 小写字母[a-z]

            [:upper:] 大写字母[A-Z]


            匹配多个重复字符:

            *    0个或任意多个

            +    1个或任意多个

            ?     0个或1个

            {n}    特定数量

            {n,}   至少n个

            {n,m}  在n个和m个之间


           SELECT prod_name

           FROM products

           WHERE prod_name REGEXP '[[:digit:]]{4}'

           ORDER BY prod_name



           位置控制字符

           ^  Start of text,字符串的开头

           $  End of text,字符串的结果

           [[:<:]]  Start of word:单词的开头

           [[:>:]]  End of word:单词的结尾


           SELECT prod_name

           FROM products

           WHERE prod_name REGEXP '^[0-9\\.]'

           ORDER BY prod_name;



第十章:创建运算过的字段

   when data stored in the table is not exactly what your

   application needs. Rather than retrieve the data as it is and then reformat it

   within your client application or report, what you really want is to retrieve

   converted, calculated, or reformatted data directly from the database

   当表中存的数据不是你的程序刚好需要的,比如同一个字段,有的是大写,有的是小写;比如电话号码格式不一样;比如你想展示一个同时包含一个公司地域和名称的字段,但地域和字段存储在不同的字段;比如你需要求得一个字段数据的平均值或总和.这时候,你可能更希望你的数据库能在提取数据后自动替你计算/格式化数据,而不需要你自己亲自动手.


   Client Versus Server Formatting :Many of the conversions and reformatting that can be performed within SQL statements can also be performed directly in your client application. However, as a rule, it is far quicker to perform these operations on the database server than it is to perform them within the client because DBMSs are built to perform this type of processing quickly and efficiently.

   许多SQL格式化或转换语句可以在服务端执行,也可以在客户端执行,但在服务器端执行会远远快于客户端,因为服务器端天生可以快速有效地进行这种操作.


   Concatenating Fields:合并域


   Most DBMSs use operators +or ||for concatenation; MariaDB (like MySQL) uses the Concat()function. Keep this in mind when converting SQL statements to MariaDB (and MySQL ).

   多数DBMS运用+或||来进行合并,但MariaDB采用concat()函数来合并


   SELECT Concat(vend_name, ' (', vend_country, ')')

   FROM vendors

   ORDER BY vend_name;


   运用别名

   The SELECT statement used to concatenate the address field works well, as seen in the previous output. But what is the name of this new calculated column? Well, the truth is, it has no name; it is simply a value. Although this can be fine if you are just looking at the results in a SQL query tool, an unnamed column cannot be used within a client application because the client has no way to refer to that column.

   SELECT语句可以输出合并后的字段,但字段的名字是什么?事实上,它没有名字,只是一个值,如果你关心的是数据的话,这个问题没什么,但客户端程序不能使用一个没有名字的字段,因为无法指向那个字段.所以可以使用Alias别名来命名合并后的字段.


   SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') AS

   vend_title

   FROM vendors

   ORDER BY vend_title;


   进行数学运算.

   SELECT prod_id,

   quantity,

   item_price,

   quantity*item_price AS expanded_price

   FROM orderitems

   WHERE order_num = 20005;

   (你懂的,懒得翻译太详细了,当然你可以使用+ - * / 等数学运算符)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章