基礎內建函式

2021-10-06 06:56:23 字數 3922 閱讀 8043

-- 四捨五入

-- round(5.1201,2) 保留兩位小數

select

round

(5.1201,2

);-- 5.12

select

truncate

(5.1201,2

);-- 截斷數字

select ceiling(

5.2)

-- 返回比傳入值大的整數:6

select floor(

5.2)

-- 返回比傳入值小且最近的整數:5

select abs(

5.2)

-- 絕對值

select rand(

)-- 獲取隨機數(0,1)

-- 獲取長度

select length(

'sky'

)-- 去除空格

select rtrim(

'sky '

)select ltrim(

' sky'

)select trim(

' sky '

)-- 擷取字串

select

left

('kindergarten',4

)-- kind

select

right

('kindergarten',6

)-- garten

select substring(

'kindergarten',3

,5)select substring(

'kindergarten ',3

)-- 定位位置

select locate(

'n',

'kindergarten'

)select locate(

'garten'

,'kindergarten'

)-- 連線字串

select concat(

'first'

,'last'

)select concat(first_ name,

' ',last_name)

asfull name

from customer

-- 替換字串

select

replace

('kindergarten'

,'garten'

,'garden'

)-- 大小寫變化

select upper(

'sky'

)select lower(

'sky'

)

-- 年月日時分秒	年月日		時分秒

select

now(

), curdate(

) ,curtime(

)-- 選取日期

select

month

(now()

)select

day(

now())

select

year

(now()

)select

hour

(now()

)select

second

(now()

)select

minute

(now()

)-- 獲取月日

select dayname(

now())

select monthname(

now())

-- 所以如果你想將你的資料匯出到其他的資料庫系統,最好是使用extract函式

select extract(

year

from

now())

-- 查詢今年的訂單

select

*from orders

where

year

(order_date)

=year

(now()

)

select date_format(

now(),

'%m %d %y'

)-- may 18 2020 (當字母大小寫變化時會有些許不同)

select date_format(

now(),

'%m %d %y'

)-- 05 18th 20

select time_format(

now(),

'%h:%i %p'

)-- 19:25 pm

-- 年月日增加減少

select date_add(

now(),

interval

1year

)-- 2021-05-18 19:35:01

select date_add(

now(),

interval-1

year

)-- 2019-05-18 19:35:12

select date_sub(

now(),

interval

1year

)-- 2019-05-18 19:35:21

-- 計算時間間隔

select datediff(

'2014-01-04'

,'2014-01-02'

)-- 2

select datediff(

'2014-01-04 7:00'

,'2014-01-02 9:00'

)-- 2

select time_to_sec(

'9:00'

)- time_to_sec(

'8:00'

)-- 3600以秒為單位

select 

concat(first_name,

' ',last_name)

as customer,

-- 替換為空的引數

ifnull(phone,

'unknown'

)as phone

-- 乙個個尋找,找到不為null則返回

-- coalesce(phone,customer_id,'unknown')

from customers

-- 產品id、名字、訂單總數、訂單頻率

select

product_id,

`name`

,count(*

)as orders,if(

count(*

)>1,

'many times'

,'once'

)as frequency

from products p

join order_items oi using

(product_id)

-- 進行分組

group

by product_id,

`name`

select 

concat(first_name,

' ',last_name)

as customer,

points,

case

when points >

3000

then

'gold'

when points >

2000

then

'silver'

else

'bronze'

endas category

from customers

order

by points desc

python基礎 內建函式

print input len type open tuple list int bool set dir id str print locals 返回本地作用域中的所有名字 print globals 返回全域性作用域中的所有名字 global 變數 nonlocal 變數 迭代器.next ne...

Python基礎 內建函式

python 直譯器內建了很多函式和型別,我們可以在任何時候使用它們。內建函式 含義abs val 求val的絕對值 all iterable 如果可迭代物件中所有的元素為真那麼就返回true,否者返回false any iterable 如果可迭代物件中有乙個元素為真那麼就返回true,如果否則返...

python基礎 內建函式

這數量有點多啊!不過有一些是前面已經用過的,還有一些是物件導向的 後面學 現在需要掌握的不算多。print abs 2 abs全稱abs 全稱absolute value,絕對值 print all 1,2,可迭代引數 iterable 中的所有元素bool是否全為true print any 1,...