国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【MySQL學習筆記】(七)內(nèi)置函數(shù)

這篇具有很好參考價值的文章主要介紹了【MySQL學習筆記】(七)內(nèi)置函數(shù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

日期函數(shù)

【MySQL學習筆記】(七)內(nèi)置函數(shù),MySQL學習筆記,mysql,學習,筆記

示例

  • 獲得當前年月日
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-09-03     |
+----------------+
1 row in set (0.00 sec)
  • 獲得當前時分秒
mysql> select current_time;
+--------------+
| current_time |
+--------------+
| 09:20:15     |
+--------------+
1 row in set (0.00 sec)

  • 獲得當前時間戳

mysql中該函數(shù)會自動將時間戳轉(zhuǎn)化為年月日時分秒的格式

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-09-03 09:20:49 |
+---------------------+
1 row in set (0.00 sec)
  • date(datetime),返回 datetime 參數(shù)中的日期部分(年月日部分)
mysql> select date(current_date());
+----------------------+
| date(current_date()) |
+----------------------+
| 2023-09-03           |
+----------------------+
1 row in set (0.00 sec)

//雖然currnt_time()獲得的只有時分秒時間,但該函數(shù)內(nèi)部獲得時間的時候是可以獲得日期(年月日)的
mysql> select date(current_time());
+----------------------+
| date(current_time()) |
+----------------------+
| 2023-09-03           |
+----------------------+
1 row in set (0.00 sec)

mysql> select date(current_timestamp());
+---------------------------+
| date(current_timestamp()) |
+---------------------------+
| 2023-09-03                |
+---------------------------+
1 row in set (0.00 sec)
  • 在日期的基礎(chǔ)上加上日期
//在 2023-9-3 的基礎(chǔ)上加上 50 天
mysql> select date_add('2023-9-3',interval 50 day);
+--------------------------------------+
| date_add('2023-9-3',interval 50 day) |
+--------------------------------------+
| 2023-10-23                           |
+--------------------------------------+
1 row in set (0.00 sec)
  • 在日期的基礎(chǔ)上減去日期
//在 2023-10-23 的基礎(chǔ)上減去 50 天
mysql> select date_sub('2023-10-23',interval 50 day);
+----------------------------------------+
| date_sub('2023-10-23',interval 50 day) |
+----------------------------------------+
| 2023-09-03                             |
+----------------------------------------+
1 row in set (0.00 sec)

  • 計算兩個日期相差多少天
mysql> select datediff('2023-9-3','2023-10-23');
+-----------------------------------+
| datediff('2023-9-3','2023-10-23') |
+-----------------------------------+
|                               -50 |
+-----------------------------------+
1 row in set (0.00 sec)

案例-1

  • 創(chuàng)建一張表,記錄生日
mysql> create table tmp (id int primary key auto_increment,birthday date not null);
Query OK, 0 rows affected (0.01 sec)

mysql> desc tmp;
+----------+---------+------+-----+---------+----------------+
| Field    | Type    | Null | Key | Default | Extra          |
+----------+---------+------+-----+---------+----------------+
| id       | int(11) | NO   | PRI | NULL    | auto_increment |
| birthday | date    | NO   |     | NULL    |                |
+----------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

  • 添加當前日期
mysql> insert into tmp(birthday) values(current_date());
Query OK, 1 row affected (0.00 sec)

mysql> select* from tmp;
+----+------------+
| id | birthday   |
+----+------------+
|  1 | 2023-09-03 |
+----+------------+
1 row in set (0.00 sec)

案例-2

  • 創(chuàng)建一個留言表
mysql> create table msg(id int primary key auto_increment,content varchar(30) not null,sendtime datetime);
Query OK, 0 rows affected (0.01 sec)

mysql> desc msg;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| content  | varchar(30) | NO   |     | NULL    |                |
| sendtime | datetime    | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
  • 插入數(shù)據(jù)
mysql> insert into msg(content,sendtime) values('hello',now());
Query OK, 1 row affected (0.00 sec)

mysql> insert into msg(content,sendtime) values('hi',now());
Query OK, 1 row affected (0.00 sec)

mysql> select* from msg;
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello   | 2023-09-03 09:48:45 |
|  2 | hi      | 2023-09-03 09:48:52 |
+----+---------+---------------------+
2 rows in set (0.00 sec)
  • 顯示所有留言信息,發(fā)布日期只顯示到年月日,不用顯示具體時間
mysql> select content ,date(sendtime) from msg;
+---------+----------------+
| content | date(sendtime) |
+---------+----------------+
| hello   | 2023-09-03     |
| hi      | 2023-09-03     |
+---------+----------------+
2 rows in set (0.00 sec)
  • 查詢在 10 分鐘內(nèi)發(fā)布的帖子
mysql> select* from msg where date_add(sendtime,interval 10 minute) > now();
+----+---------+---------------------+
| id | content | sendtime            |
+----+---------+---------------------+
|  1 | hello   | 2023-09-03 09:48:45 |
|  2 | hi      | 2023-09-03 09:48:52 |
+----+---------+---------------------+
2 rows in set (0.00 sec)
理解:

------------------------------|-----------|-------------|------------------

                           初始時間     now()       初始時間+10min  

字符串函數(shù)

【MySQL學習筆記】(七)內(nèi)置函數(shù),MySQL學習筆記,mysql,學習,筆記

示例

  • 獲取表中的某一列的字符集
mysql> select charset(content) from msg;
+------------------+
| charset(content) |
+------------------+
| utf8             |
| utf8             |
+------------------+
2 rows in set (0.00 sec)
  • 字符串連接
mysql> select concat('app','le');
+--------------------+
| concat('app','le') |
+--------------------+
| apple              |
+--------------------+
1 row in set (0.00 sec)
  • 某字符串中是否包含某子串,包含則返回出現(xiàn)的位置,未包含則返回 0
mysql> select instr('apple','ple');
+----------------------+
| instr('apple','ple') |
+----------------------+
|                    3 |
+----------------------+
1 row in set (0.00 sec)

mysql> select instr('apple','b');
+--------------------+
| instr('apple','b') |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec)
  • 轉(zhuǎn)換成大寫
mysql> select ucase('abcA');
+---------------+
| ucase('abcA') |
+---------------+
| ABCA          |
+---------------+
1 row in set (0.00 sec)
  • 轉(zhuǎn)換成小寫
mysql> select lcase('abcA');
+---------------+
| lcase('abcA') |
+---------------+
| abca          |
+---------------+
1 row in set (0.00 sec)
  • 從字符串的左邊或者右邊起取 length 個字符
mysql> select left('abcdef',2);
+------------------+
| left('abcdef',2) |
+------------------+
| ab               |
+------------------+
1 row in set (0.00 sec)

mysql> select right('abcdef',2);
+-------------------+
| right('abcdef',2) |
+-------------------+
| ef                |
+-------------------+
1 row in set (0.00 sec)
  • 獲取字符串的長度
mysql> select length('abc');
+---------------+
| length('abc') |
+---------------+
|             3 |
+---------------+
1 row in set (0.00 sec)

mysql> select length('你好');
+------------------+
| length('你好')   |
+------------------+
|                6 |
+------------------+
1 row in set (0.00 sec)

注意:length函數(shù)返回字符串長度,以字節(jié)為單位。如果是多字節(jié)字符則計算多個字節(jié)數(shù);
如果是單字節(jié)字符則算作一個字節(jié)。比如:字母,數(shù)字算作一個字節(jié),中文表示多個字節(jié)數(shù)
(與字符集編碼有關(guān))

  • 在字符串中用某子串替換掉其中的子串
//在 ‘hello world’ 中用 ‘jack’ 替換掉 ‘world’
mysql> select replace('hello world','world','jack');
+---------------------------------------+
| replace('hello world','world','jack') |
+---------------------------------------+
| hello jack                            |
+---------------------------------------+
1 row in set (0.00 sec)
  • 逐字符比較字符串的大小
//第一個字符串小于第二個字符串返回 -1
mysql> select strcmp('abc','abd');
+---------------------+
| strcmp('abc','abd') |
+---------------------+
|                  -1 |
+---------------------+
1 row in set (0.00 sec)

//相等返回 0
mysql> select strcmp('abc','abc');
+---------------------+
| strcmp('abc','abc') |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.00 sec)

//大于返回 1
mysql> select strcmp('abd','abc');
+---------------------+
| strcmp('abd','abc') |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.00 sec)
  • 從字符串的第 position 開始,取 length 個字符
//從第一個位置開始取三個字符
mysql> select substring('abcdefg','1',3);
+----------------------------+
| substring('abcdefg','1',3) |
+----------------------------+
| abc                        |
+----------------------------+
1 row in set (0.00 sec)
  • 去除 前空格 / 后空格 / 前后空格
mysql> select '   hello   ' as res;
+-------------+
| res         |
+-------------+
|    hello    |
+-------------+
1 row in set (0.00 sec)

mysql> select ltrim('   hello   ') as res;//去除前空格
+----------+
| res      |
+----------+
| hello    |
+----------+
1 row in set (0.00 sec)

mysql> select rtrim('   hello   ') as res;//去除后空格
+----------+
| res      |
+----------+
|    hello |
+----------+
1 row in set (0.00 sec)

mysql> select trim('   hello   ') as res; //去除前后空格
+-------+
| res   |
+-------+
| hello |
+-------+
1 row in set (0.00 sec)

數(shù)學函數(shù)

【MySQL學習筆記】(七)內(nèi)置函數(shù),MySQL學習筆記,mysql,學習,筆記文章來源地址http://www.zghlxwxcb.cn/news/detail-696417.html

  • 絕對值
mysql> select abs(-100.5);
+-------------+
| abs(-100.5) |
+-------------+
|       100.5 |
+-------------+
1 row in set (0.00 sec)
  • 向上取整
mysql> select ceiling(23.4);
+---------------+
| ceiling(23.4) |
+---------------+
|            24 |
+---------------+
1 row in set (0.00 sec)

  • 向下取整
mysql> select floor(24.7);
+-------------+
| floor(24.7) |
+-------------+
|          24 |
+-------------+
1 row in set (0.00 sec)

  • 保留小數(shù)位數(shù)
//保留2位小數(shù)位數(shù)(小數(shù)四舍五入)
mysql> select format(12.3456,2);
+-------------------+
| format(12.3456,2) |
+-------------------+
| 12.35             |
+-------------------+
1 row in set (0.00 sec)

  • 產(chǎn)生隨機數(shù)
mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.2013088168588549 |
+--------------------+
1 row in set (0.00 sec)

mysql> select rand();
+--------------------+
| rand()             |
+--------------------+
| 0.2613807602425858 |
+--------------------+
1 row in set (0.00 sec)

其他函數(shù)

  • user() 查詢當前用戶
  • md5(str)對一個字符串進行md5摘要,摘要后得到一個32位字符串
  • database()顯示當前正在使用的數(shù)據(jù)庫
  • password()函數(shù),MySQL數(shù)據(jù)庫使用該函數(shù)對用戶加密
  • ifnull(val1, val2) 如果val1為null,返回val2,否則返回val1的值

到了這里,關(guān)于【MySQL學習筆記】(七)內(nèi)置函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 《MySQL》內(nèi)置函數(shù)

    《MySQL》內(nèi)置函數(shù)

    函數(shù)名稱 功能 current_date() 當前日期(yyyy-mm-dd) current_time() 當前時間(hh-mm-ss) current_timestamp() 當前時間戳(yyyy-mm-dd hh-mm-ss) now() 當前日期時間 date(datetime) 返回datetime參數(shù)的日期部分 date_add(date, interval d_value_type) 返回date加上d的日期時間,d可以是天、時、分、秒 date_sub(dat

    2024年02月13日
    瀏覽(33)
  • 【MySQL】內(nèi)置函數(shù)

    【MySQL】內(nèi)置函數(shù)

    日期函數(shù) 獲得年月日: 獲得時分秒: 獲得時間戳: 同時使用 current_date 和 current_time 就相當于 current_timestamp 在日期的基礎(chǔ)上加日期: 在日期的基礎(chǔ)上減去時間: 計算兩個日期之間相差多少天: 獲取時間戳中的日期 創(chuàng)建生日表 創(chuàng)建留言表 字符串函數(shù) 查看字符串的字符集

    2024年02月15日
    瀏覽(19)
  • MySQL學習筆記 ------ 常見函數(shù)

    //----------常見函數(shù)----------// ? 功能:類似于java中的方法; ? 好處:提高重用性和隱藏實現(xiàn)細節(jié); ? 調(diào)用:SELECT 函數(shù)名(實參列表); 1、字符函數(shù) (1)LENGTH :獲取字節(jié)個數(shù)(utf-8一個漢字代表3個字節(jié),gbk為2個字節(jié)) (2)CONCAT :拼接字符串 (3)SUBSTR :截取子串(注意:索引從

    2024年02月15日
    瀏覽(16)
  • MySQL學習筆記 ------ 分組函數(shù)

    //----------分組函數(shù)----------// 1、功能 ????????用作統(tǒng)計使用,又稱為聚合函數(shù)或統(tǒng)計函數(shù)或組函數(shù) 2、分類 ????????sum 求和、avg 平均值、max 最大值 、min 最小值 、count 計算個數(shù) 3、特點 (1)sum、avg一般用于處理數(shù)值型;max、min、count可以處理任何類型; (2)以上分組

    2024年02月15日
    瀏覽(20)
  • MySQL數(shù)據(jù)庫:內(nèi)置函數(shù)

    MySQL數(shù)據(jù)庫:內(nèi)置函數(shù)

    規(guī)定:日期:年月日? ? ? ?時間:時分秒 函數(shù)名稱 作用描述 current_date() 當前日期 current_time() 當前時間 current_timestamp() 當前時間戳 date(datetime) 返回datetime參數(shù)的日期部分 date_add(date,interval d_value_type) 在date中添加時間或日期。interval后面可以是year、day、minute、second date_sub(da

    2024年02月11日
    瀏覽(24)
  • MySQL基本查詢與內(nèi)置函數(shù)

    MySQL基本查詢與內(nèi)置函數(shù)

    目錄 聚合函數(shù) ?分組查詢 ?內(nèi)置函數(shù) 日期函數(shù) 字符串函數(shù) 數(shù)學函數(shù) COUNT:返回查詢到的數(shù)據(jù)的數(shù)量 SUM:返回查詢到的數(shù)據(jù)的總和(數(shù)字) AVG:返回數(shù)據(jù)的平均值 MAX:返回查詢到的數(shù)據(jù)的最大值 MIN:返回查詢到的數(shù)據(jù)的最小值 ? ?count ? ?sum ? ?max,avg 準備三個表: 部門

    2024年02月13日
    瀏覽(42)
  • Mysql日期格式及內(nèi)置日期函數(shù)

    Mysql日期格式及內(nèi)置日期函數(shù)

    MySQL中常用的幾種時間類型有:date、datetime、time、year、timestamp datetime和timestamp的區(qū)別 相同點 : 存儲格式相同 datetime和timestamp 兩者的時間格式都是YYYY-MM-DD HH:MM:SS 不同點 : a. 存儲范圍不同. datetime的范圍是1000-01-01到9999-12-31. 而timestamp是從1970-01-01到2038-01-19, 即后者的時間范圍很

    2023年04月09日
    瀏覽(20)
  • MySQL:內(nèi)置函數(shù)、復合查詢和內(nèi)外連接

    MySQL:內(nèi)置函數(shù)、復合查詢和內(nèi)外連接

    select? 函數(shù); ? 實際開發(fā)中往往數(shù)據(jù)來自不同的表,所以需要多表查詢。本節(jié)我們用一個簡單的公司管理系統(tǒng),有三張 表EMP,DEPT,SALGRADE來演示如何進行多表查詢。 如果說 表1有n條記錄,表2有m條記錄,那么笛卡兒積之后就有n * m條記錄。 在實際應用中,為了合并多個select的執(zhí)

    2024年02月13日
    瀏覽(19)
  • 學習筆記-mysql-各種函數(shù)的基本使用

    count , sum , min , max ,avg , group_concat() (1). if邏輯判斷 (2). case when mysql 8.0之后增加的,也稱為開窗函數(shù) (1). 序號函數(shù) row_number( ) --排序 1,2,3 rank( ) --排序 1,1,3 dense_rank( ) --排序 1,1,2 另外還有開窗聚合函數(shù):sum avg min max (2). 分布函數(shù) cume_dist() 用途:分組內(nèi)小于、等于當前rank值的行數(shù)

    2024年01月20日
    瀏覽(24)
  • MySQL學習筆記3——條件查詢和聚合函數(shù)

    MySQL學習筆記3——條件查詢和聚合函數(shù)

    WHERE 和 HAVING 的區(qū)別: WHERE是直接對表中的字段進行限定,來篩選結(jié)果; HAVING則需要跟分組GROUP BY一起使用,通過對分組字段或分組計算函數(shù)進行限定,來篩選結(jié)果。 雖然它們都是對查詢進行限定,卻有著各自的特點和適用場景。 WHERE WHERE的特點是,直接用表的

    2024年04月16日
    瀏覽(41)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包