程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 賢者時間太久了麼?--MySQL繼續玩

賢者時間太久了麼?--MySQL繼續玩

編輯:MySQL綜合教程

賢者時間太久了麼?--MySQL繼續玩


1、MySQL   -----運算符和函數-----   字符函數,數值運算符,比較運算等   ----字符函數   ---   CONCAT()--字符連接   CONCAT_WS()--使用指定的分隔符進行字符連接   mysql> SELECT CONCAT('imooc','-','MySQL'); +-----------------------------+ | CONCAT('imooc','-','MySQL') | +-----------------------------+ | imooc-MySQL | +-----------------------------+   mysql> USE TEST; Database changed mysql> SELECT * FROM TEST; +----+----------+ | id | username | +----+----------+ | 1 | Tom | | 2 | Ben | +----+----------+ 2 rows in set (0.02 sec)   mysql> SELECT CONCAT(id,username) AS fullname FROM test; +----------+ | fullname | +----------+ | 1Tom | | 2Ben | +----------+   以上是CONCAT()的實例,而CONCAT_WS()如下   mysql> SELECT CONCAT_WS('-',id,username) FROM test; +----------------------------+ | CONCAT_WS('-',id,username) | +----------------------------+ | 1-Tom | | 2-Ben | +----------------------------+   CONCAT_WS()需要至少三個參數,第一個是分隔符,後面才是需要連接的東西   ---   FORMAT(),數字格式化   mysql> SELECT FORMAT(234234.23423,2); +------------------------+ | FORMAT(234234.23423,2) | +------------------------+ | 234,234.23 | +------------------------+   就是標准化,當然是外國人那一套   ---   LOWER()&UPPER(),大小寫變換   ---   LEFT()&RIGHT(),獲取左右側字符   需要兩位參數,從哪個字段取,從第幾位開始的左右側   mysql> SELECT UPPER(LEFT('andy',1)); +-----------------------+ | UPPER(LEFT('andy',1)) | +-----------------------+ | A | +-----------------------+   ---   LENGTH(),獲取字符串長度。注意,空格也要算在內   ---   LTRIM(),RTRIM(),TRIM(),刪除前/後/前後的空格   TRIM還能刪除其他字符   mysql> SELECT TRIM(LEADING '!' FROM '!!!ANDY!!'); +------------------------------------+ | TRIM(LEADING '!' FROM '!!!ANDY!!') | +------------------------------------+ | ANDY!! | +------------------------------------+ 1 row in set (0.00 sec)   mysql> SELECT TRIM(BOTH '!' FROM '!!!ANDY!!'); +---------------------------------+ | TRIM(BOTH '!' FROM '!!!ANDY!!') | +---------------------------------+ | ANDY | +---------------------------------+ 1 row in set (0.00 sec)   mysql> SELECT TRIM(TRAILING '!' FROM '!!!ANDY!!'); +-------------------------------------+ | TRIM(TRAILING '!' FROM '!!!ANDY!!') | +-------------------------------------+ | !!!ANDY | +-------------------------------------+ 1 row in set (0.00 sec)   ---   REPLACE()   mysql> SELECT REPLACE('!!ANDY!SDL!!','!',''); +--------------------------------+ | REPLACE('!!ANDY!SDL!!','!','') | +--------------------------------+ | ANDYSDL | +--------------------------------+ 1 row in set (0.00 sec)   mysql> SELECT REPLACE('!!ANDY!SDL!!','!','LALAL'); +-------------------------------------+ | REPLACE('!!ANDY!SDL!!','!','LALAL') | +-------------------------------------+ | LALALLALALANDYLALALSDLLALALLALAL | +-------------------------------------+ 1 row in set (0.00 sec)   可以看到,替換是比較靈活的   ---   SUBSTRING()   mysql> SELECT SUBSTRING('MYSQL','1','2'); +----------------------------+ | SUBSTRING('MYSQL','1','2') | +----------------------------+ | MY | +----------------------------+ 1 row in set (0.00 sec)   注意mysql是從1開始,不是從0開始數數   ---   做匹配   mysql> SELECT 'mysql' LIKE 'M%'; +-------------------+ | 'mysql' LIKE 'M%' | +-------------------+ | 1 | +-------------------+ 1 row in set (0.00 sec)   mysql> SELECT * FROM test WHERE username LIKE '%m%'; +----+----------+ | id | username | +----+----------+ | 1 | Tom | +----+----------+   這裡,%代表任意,類似window中的*   _代表任意一個字符   ----數值運算符以及函數   幾個很通用的函數簡單的介紹下   CEIL()-向上取整---------FLOOR()-向下取整   DIV()-整數除法,也就是結果只有整數   MOD()-取余數   POWER()-冪運算   ROUND()-四捨五入   TRUNCATE()-數字截取(不再四捨五入)   不是兩位參數,就是一位參數,大家自己試試哈   ----比較運算符與函數   ---   [NOT] BETWEEN...AND...   mysql> SELECT 123 BETWEEN 2 AND 123123; +--------------------------+ | 123 BETWEEN 2 AND 123123 | +--------------------------+ | 1 | +--------------------------+   ---   [NOT] IN(),給定幾個區間來做判斷   mysql> SELECT 123 IN(123,23,12); +-------------------+ | 123 IN(123,23,12) | +-------------------+ | 1 | +-------------------+ 1 row in set (0.00 sec)   mysql> SELECT 123 IN(120,23,12); +-------------------+ | 123 IN(120,23,12) | +-------------------+ | 0 | +-------------------+   ---   IS [NOT] NULL,判斷是空麼   mysql> SELECT 0 IS NULL; +-----------+ | 0 IS NULL | +-----------+ | 0 | +-----------+   常用在檢查是否為空   mysql> SELECT * FROM test WHERE username IS NULL; Empty set (0.00 sec)   ----日期時間函數   ---   NOW(),返回當時的日期和時間   CURDATE(),CURTIME()   ---   DATE_ADD(),當前日期的變化   mysql> SELECT DATE_ADD('2012-2-23', INTERVAL 234 DAY); +-----------------------------------------+ | DATE_ADD('2012-2-23', INTERVAL 234 DAY) | +-----------------------------------------+ | 2012-10-14 | +-----------------------------------------+ 1 row in set (0.00 sec)   mysql> SELECT DATE_ADD('2012-2-23', INTERVAL -234 DAY); +------------------------------------------+ | DATE_ADD('2012-2-23', INTERVAL -234 DAY) | +------------------------------------------+ | 2011-07-04 | +------------------------------------------+ 1 row in set (0.00 sec)   ---   DATEDIFF(),得到兩個日期間的差值   mysql> SELECT DATEDIFF('2304-2-2','1234-3-22'); +----------------------------------+ | DATEDIFF('2304-2-2','1234-3-22') | +----------------------------------+ | 390760 | +----------------------------------+ 1 row in set (0.02 sec)   ---   DATE_FORMAT(),日期的格式化,日期的格式是可以選的,也就是說日期的格式轉換   mysql> SELECT DATE_FORMAT('2013-2-22','%m/%d/%y'); +-------------------------------------+ | DATE_FORMAT('2013-2-22','%m/%d/%y') | +-------------------------------------+ | 02/22/13 | +-------------------------------------+ 1 row in set (0.00 sec)   ----信息函數   USER(),VERSION(),DATEBASE(),CONNECTION_ID(),LAST_INSERT_ID()   ----聚合函數   只有一個返回值是他們的特點   AVG(),COUNT(),MAX(),MIN(),SUM()   直接調用可能會有錯誤,一般是針對數據表的字段進行的操作   mysql> SELECT AVG(id) FROM test; +---------+ | AVG(id) | +---------+ | 1.5000 | +---------+   ----加密函數   MD5(),PASSWORD()   mysql> SELECT MD5('HELLOWORLD'); +----------------------------------+ | MD5('HELLOWORLD') | +----------------------------------+ | e81e26d88d62aba9ab55b632f25f117d | +----------------------------------+ 1 row in set (0.00 sec)   mysql> SELECT PASSWORD('HELLOWORLD'); +-------------------------------------------+ | PASSWORD('HELLOWORLD') | +-------------------------------------------+ | *3456E7782A7F539BC823C715DB60231B0C7DE847 | +-------------------------------------------+ 1 row in set (0.00 sec)   一般而言,網頁的編程用的都是MD5,password僅僅用於修改當前用戶的密碼   ----   注重自帶函數的熟悉、了解,靈活的調用和嵌套運用

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved