MySQL中的運算符、經常使用函數

MySQL中的運算符

算術運算符

image-20200215123410519

SELECT 0.1+0.333,0.1-0.333,0.1*0.333,1/2,1%2;

其中在除法運算和模運算中,除數爲0,將是非法數,返回結果爲NULLmysql

select 1/0,100%0;

image-20200215123825306

對於模運算,還有另一種表達方式,使用MOD(a,b)函數與a%b效果同樣sql

SELECT 3%2,mod(3,2)

比較運算符

image-20200215125121767

image-20200215125137230

比較運算符能夠用於比較數字、字符串和表達式。數字做爲浮點數比較,函數

字符串以不區分大小寫的方式比較。spa

  • NULL不能用於等於比較
SELECT 1=0,1=1,NULL=NULL;

image-20200215125641622

  • "<>"運算符,和"="相反。"NULL"不能用於「<>」比較
  • 「<=>」運算符,和"="相似,在操做數相等時值爲1,不一樣之處在於能夠用於NULL 的比較。

"LIKE"運算符的使用格式爲"a LIKE %123%", 當a中含有123時返回爲1,不然返回03d

select 10 between 10 and 20, 9 between 10 and 20;
select 1 in (1,2,3), 't' in ('t','a','b','l','e'), 0 in (1,2)
SELECT 0 is null, null is null;
select 0 is not null, null is not null;
SELECT 123456 like '123%', 123456 like '%123%', 123456 like '%321%';

「regexp」運算符的使用格式爲「str regexp str_pat」,當str字符串中含有str_pat相匹配的字符串時,則返回值爲1,不然返回爲0.code

SELECT 'abcdef' regexp 'ab', 'abcdefg' regexp 'k'

邏輯運算符

邏輯運算符(布爾運算符)regexp

image-20200215134331382

位運算符

位運算是指將給定的操做數轉化爲二進制後,對各個操做數的每一位都進行邏輯運算。blog

image-20200215134627571

select 2&3;
select 2&3&4;
select 2|3;
select 2^3;
select ~1, ~18446744073709551614;
SELECT bin(18446744073709551614);
select 100>>3;
select 100<<3

經常使用函數

字符串函數

image-20200215140915396

image-20200215140932942

一些常見字符串函數運行結果:ci

任何字符串與NULL鏈接都是NULL字符串

select concat('aaa','bbbb','cccc'),concat('aaa',null);

image-20200215140740263

select insert('beijing2008you',12,3,'me')

image-20200215141121059

SELECT LOWER('BEIJING2008'), UPPER('bejing2008');

image-20200215151006587

select left('beijing2008',7),LEFT('beijing',NULL),RIGHT("beijing2008",4)

image-20200215154640659

select ltrim(' |beijing'),rtrim('beijing|   ')

image-20200215154929335

select repeat('mysql',3)

image-20200215155026555

select replace('beijng_2020','_2020','2008')

image-20200215155118023

select trim(' $ beijing2008 $  ')

image-20200215155234066

select substring('beijng2008',8,4),substring('beijng2008',1,7)

image-20200215155434084

數值函數

image-20200215155610196

select ABS(-0.8), ABS(0.8)

Screenshot 2020-02-15 at 15.57.21

SELECT CEIL(-0.8),CEIL(0.8)

image-20200215160742157

SELECT FLOOR(-0.8),FLOOR(0.8)

image-20200215160848892

SELECT MOD(15,10),MOD(1,11),MOD(NULL,10);

image-20200215161020005

SELECT ceil(100*rand()),ceil(100*rand());

image-20200215161146843

select round(1.1),round(1.1,2),round(1.0,3);

image-20200215161408673

select round(1.235,2),truncate(1.235,2)

image-20200215161506574

流程函數

流程函數用來提升語句效率

image-20200215162940961

CREATE table salary(userid int, salary decimal(9,2))
INSERT INTO salary VALUES(1,1000),(2,2000),(3,3000),(4,4000),(5,null)
SELECT * FROM salary

image-20200215164722576

select if(salary>2000,'high','low') from salary;

image-20200215164651256

select ifnull(salary,0) from salary;

image-20200215165312715

select case when salary<=2000 then 'low' else 'high' end from salary;

image-20200215165714256

日期和時間函數

image-20200215161649576

其餘經常使用函數

image-20200215161907547

相關文章
相關標籤/搜索