Mysql運算符與函數(胖胖老師)

来源:http://www.cnblogs.com/wadmwz/archive/2017/09/20/7558776.html
-Advertisement-
Play Games

use test;create table `employee`( emp_no int unsigned, emp_name varchar(30), emp_sex varchar(3), emp_age tinyint unsigned, sal double, history datetim ...



use test;
create table `employee`(
    emp_no int unsigned,
    emp_name varchar(30),
    emp_sex varchar(3),
    emp_age tinyint unsigned,
    sal double,
    history datetime
);
insert into employee values(1, '張三', '男', 18, 5000, '2012-04-23'),
(2, '李四', '男', 27, 4500, '2013-05-23'),
(3, '王五', '男', 23, 4700, '2012-04-21'),
(4, '子龍', '男', 19, 3800, '2011-03-04'),
(5, '李白', '男', 15, 6200, '2015-09-09'),
(6, '劉備', '男', 28, 2500, '2016-02-11'),
(7, '呂布', '男', 21, 6000, '2010-10-18'),
(8, '尚香', '女', 16, 4500, '2011-09-26'),
(9, '小喬', '女', 15, null, '2013-07-05'),
(10, '大喬', '女', 16, 5000, '2017-09-01');


常用的運算符:
1: 等於( = )
    select * from employee where sal = 3800;
    select * from employee where sal = null;     --這裡查詢不到為null的數據

2: 等於( <=> )
    select * from employee where sal <=> 3800;
    select * from employee where sal <=> null;   --這裡可以查詢到為null的數據

3: is判斷(null)
    select * from employee where sal is null;
    select * from employee where sal is not null;

4: null值判斷還可以使用isnull();
    select * from employee where isnull(sal);
    select * from employee where !isnull(sal);

5: 在區間(between)內  between min and max  ps:這裡是一個閉區間
    select * from employee where sal between 4500 and 5000;

6: 不在區間內
    select * from employee where sal not between 4500 and 5000;  --null不為包括進去

7: and 和 or
    select * from employee where sal not between 4500 and 5000 or sal is null;
    select * from employee where sal = 4500 and emp_sex = '女';

8: 小於(<), 大於(>), 小於等於(<=), 大於等於(>=)
    select * from employee where sal >= 4500;
***************************************************************************************************************

數學函數
1: rand();
    select rand() from dual;   --dual是一個偽表
    select 1+1 from dual;
    select rand();   --可以簡寫

2: least(value1, value2, ...) 返回最小值
    select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
    select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76) as min_value;   --列名可以起一個別名

3: greatest(value1, value2, ...) 返回最大值
    select greatest(54,76,4,65,76,87,87,56,65,654,45,23,1,76);

4: round(M, D); 返回M的四捨五入的值, D表示要保留幾們小數,預設值是0
    select round(1.69);
    select round(1.69, 1);

5: abs() 絕對值
    select 5-10;
    select abs(5-10);
***************************************************************************************************************

彙總函數
1: avg();
    select * from employee where sal >= 6000;
    select avg(sal) from employee where sal >= 6000;

2: count()
    select count(*) from employee;
    select count(emp_name) from employee;
    select count(sal) from employee;      --列印9 這裡會忽略null值
    select count(*) from employee where sal >= 4000;
    select count(*) from employee where sal <= 4000 or sal is null;

3: sum()
    select sum(sal) from employee where sal >= 6000;

4: min()
    select min(sal) from employee;

5: max()
    select max(sal) from employee;
***************************************************************************************************************

日期函數
1: 獲取當前的日期時間
    select now(), sysdate(), current_timestamp();
    select now(6), sysdate(6), current_timestamp(6);
    ps: now(), current_timestamp();沒有區別, 表示sql開始執行時的時間
        sysdate()表示這個函數開始時間

2: 獲取當前日期
    select curdate();   --只有年月日

3: 獲取當前時間
    select curtime();   --只有時分秒

4: 日期的加運算date_add     
    select history, date_add(history, interval '1 12:10' day_minute) from employee;   --date_add(history, interval '1 12:10' day_minute)
    select history, date_add(history, interval '1-1' year_month) from employee;       --date_add(history, interval '1-1' year_month)
    select history, date_add(history, interval '1' second) from employee;             --date_add(history, interval '1' second)

5: 日期的減運算data_sub
    select history, date_sub(history, interval '1-1' year_month) from employee;  

6: 計算日期差
    select history, sysdate(), datediff(sysdate(), history) from employee;     --以天數來表示

7: 獲取日期的指定部分(把日期轉換為指定的格式)  date_format()
    select history, date_format(history, '%Y年%m月%d號') from employee;
    select history, date_format(history, '%d號') from employee;
    select history, date_format(history, '%Y年%m月%d號 %H時%i分%s秒') from employee;

8: 計算出一個日期是星期幾
    select history, dayname(history) from employee;

9: 中文日期字元串轉換日期str_to_date()
    insert into employee values(11, '張飛', '男', 22, 3000, '2017年02月01號');   --報錯
    insert into employee values(11, '張飛', '男', 22, 3000, str_to_date('2017年02月01號', '%Y年%m月%d號 %H時%i分%s秒'));

    insert into employee values(12, '二哥', '男', 22, 3000, str_to_date('2017年02月01號 23時02分02秒', '%Y年%m月%d號 %H時%i分%s秒'));
    insert into employee values(12, '二哥', '男', 22, 3000, str_to_date('2017年02月01號 11時02分02秒', '%Y年%m月%d號 %h時%i分%s秒'));
    ps: 如果是h則表示12小制, 如果是大H則表示24小明制;



字元串函數
1: left(str, len) 返回字元串str的左端len個字元
    select left('abcdefg', 5);
 
2: length()
    select length('abcdefg');

3: lower(str) 返回小寫的字元串str
    select lower('HELLO');

4: substring() 取子字元串, 第二個參數是截取的起始位置, 第三個參數是要截取的長度
    select substring('helloworld',2,3);

5: concat() 字元串拼接
    select concat(emp_name, '員工') from employee;

6: replace(替換
    select replace(emp_name, '李', '老') from employee where emp_name = '李四';



您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 產品一直催我在 RN 項目中添加分享功能,一直沒找到合適的庫,今天讓我看到了一個 "插件" 分享給大家。 在集成插件之前,需要在各大開放平臺上成功註冊應用,並通過審核(支持 3 個可選的主流平臺)。支持的平臺如下: "微信開放平臺" "QQ 開放平臺" "微博開放平臺" 第一步 安裝: 在你的項目路 ...
  • NOTE: The below code may not contain all device's string, I'm with other guys are maintaining the same code on GitHub so please take the latest code f ...
  • Android精選源碼 輕量級底部導航欄 android手機衛士源碼 android實現高仿今日頭條源碼 一個用Kotlin寫的簡單漫畫App源碼 android吐槽項目完整源碼 實現可以滑動文字逐漸變色的TabLayout android實現將app隱藏加密功能的源碼 android實現橫向滾動的 ...
  • 以下內容由Mockplus團隊翻譯整理,僅供學習交流,Mockplus是更快更簡單的原型設計工具。 註意:本文中提到的設計方法在不同設計場景帶來的效果是不一樣的,並不能說是好的或不好的。 這意味著,如果你正確執行,它們都具有很大的潛力。 1. 彈窗 也許你有過這種經歷。你打開一個新的網頁,閱讀裡面的 ...
  • iOS精選源碼 iOS優質博客 !(/data/attachment/album/201709/09/091733q7q452x972sq2890.png)最近剛剛把接手的OC項目搞定,經過深思熟慮後,本人決定下個項目起就使用Swift(學了這麼久的Swift還沒真正用到實際項目里。。。),而恰巧R ...
  • View繪製的三部曲,測量,佈局,繪畫現在我們分析繪畫部分測量和佈局 在前兩篇文章中已經分析過了。不瞭解的可以去我的博客里找一下 下麵進入正題,開始分析調用以及函數原理 這個函數調用內部draw方法去處理繪畫前的工作,來繼續完成繪製工作 這個函數在調用view的draw之前做了很多處理,大概總結一下 ...
  • 轉自CSDN: 本文會從小程式前端開發,小程式服務端開發及小程式的發佈與審核三個方面來闡述小程式的開發流程。 一、小程式前端介紹及開發 小程式的開發涉及到前端開發和後端開發,前端指的是在手機上能看到的部分,主要負責頁面的佈局排版及展示,後端提供數據和業務處理能力,指的是我們寫給前端調用的API介面。 ...
  • 如果你的項目什麼也不修改,直接把你的app運行在 iPhone X 模擬器下,很有可能就會出現下麵的情形: 上下都有黑邊,沒有全屏顯示 為了讓app能夠全屏顯示,你需要準備以下的內容 Xcode 9.0 [email protected],尺寸為 1125px * 2436px 配置 Brand Ass ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...