<<連載>><<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;

   (你懂的,懶得翻譯太詳細了,當然你可以使用+ - * / 等數學運算符)


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