MYSQL 常用內建方法整理

2021-08-28 22:41:49 字數 2352 閱讀 3912

大於號》

>

小於號<

< 與&

&單引號"'

雙引號'

"

## 從左開始擷取字串 

left(str, length)

## 說明:left(被擷取字段,擷取長度)

## 例:select left(content,200) as abstract from my_content_t

## 從右開始擷取字串

right(str, length)

## 說明:right(被擷取字段,擷取長度)

## 例:select right(content,200) as abstract from my_content_t

## 擷取字串

substring(str, pos)

substring(str, pos, length)

## 說明:substring(被擷取字段,從第幾位開始擷取)

substring(被擷取字段,從第幾位開始擷取,擷取長度)

## 例:select substring(content,5) as abstract from my_content_t

select substring(content,5,200) as abstract from my_content_t

## (注:如果位數是負數 如-5 則是從後倒數字數,到字串結束或擷取的長度)

## 按關鍵字擷取字串

substring_index(str,delim,count)

## 說明:substring_index(被擷取字段,關鍵字,關鍵字出現的次數)

## 例:select substring_index(」blog.jb51.net」,」。」,2) as abstract from my_content_t

## 結果:blog.jb51

## (注:如果關鍵字出現的次數是負數 如-2 則是從後倒數,到字串結束)

concat(str1,str2,…)
uuid()
now()
distinct (過濾字段)
count(計數字段);
sum(求和字段);
date_format( 日期字段, '%y-%m-%d' ) =0
//包含當天

select * from 表名 where datediff(字段,now())<=0 and datediff(字段,now())>-n

//不包含當天

select * from 表名 where datediff(字段,now())<0 and datediff(字段,now())>-n

#mysql查詢今天、昨天、7天、近30天、本月、上一月 資料

#今天select * from 表名 where to_days(時間欄位名) = to_days(now());

#昨天select * from 表名 where to_days( now( ) ) – to_days( 時間欄位名) <= 1

#7天select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名)

#近30天

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名)

#本月select * from 表名 where date_format( 時間欄位名, 『%y%m』 ) = date_format( curdate( ) , 『%y%m』 )

#上一月

select * from 表名 where period_diff( date_format( now( ) , 『%y%m』 ) , date_format( 時間欄位名, 『%y%m』 ) ) =1

datediff() 函式用於返回兩個日期之間的天數。 語法:datediff(date1,date2)date1 和 date2

引數是合法的日期或日期/時間表示式。 注釋:

1. 只有值的日期部分參與計算。

2. 當日期date1date2時函式返回值為負數。

3. mysql的datediff只有兩個引數。sql server有三個引數,詳細內容可見:sql date函式

mysql內建變數 MySQL常用內建變數

mysql用很多常用的內建變數,掌握這些內建變數後對於我們快速獲取當前mysql的配置有很大幫助,下面就來列舉幾個常用的變數。檢視當前mysql版本號資訊。show variables like version mariadb none show variables like version var...

mysql常用內建函式 mysql常見內建函式

在mysql中有許多內建的函式,雖然功能都能在php 中實現,但巧妙的應用mysql內建函式可以大大的簡化開發過程,提高效率。mysql字串函式 concat string2 連線字串 lcase string2 轉換成小寫 ucase string2 轉換成大寫 length string str...

mysql常見內建函式 MySQL常用內建函式

1 ascii str 返回字串str的第乙個字元的ascii值 str是空串時返回0 mysql select ascii 2 50 mysql select ascii 2 50 mysql select ascii dete 100 2 ord str 如果字串str句首是單位元組返回與asc...