數(shù)據(jù)庫(kù)高級(jí)查詢

????????????????????????????????????????????????????????????????????????????????????????????查詢相關(guān)

1.判斷某個(gè)屬性為null時(shí),用is null;

2.判斷某個(gè)屬性為非null時(shí),用 is not null;

3.別名 :

selcet sname as '姓名',sal as '工資' from emp

? selcet sname '姓名',sal '工資' from emp

selcet sname 姓名,sal 工資 from emp

4.去掉重復(fù)的distinct,把distinct加到去掉重復(fù)的字段之前

查詢所有的職位去掉重復(fù)的:

select distinct job from emp;

5.and 和java中的&&一樣? or和java中||一樣

6.查詢工資為1500,5000,3000的員工信息

select * from emp where sal=1500||sal=3000||sal=5000;

select * from emp where sal in (1500,3000,5000);

7.between x and y 在x和y之間,包含x,y

查詢工資在2000到4000之間的員工信息(包含2000和4000)

select * from emp where sal between 2000 and 4000;

查詢工資小于2000,大于4000的員工信息

select * from emp where sal not between 2000 and 4000;

8.模糊查詢 like

_ 代表單個(gè)未知字符? ? ? ? ? ? %代表0或多個(gè)未知字符

舉例:

以a開頭? a%? ? ? ? ? ? 以a結(jié)尾? %a? ? ? ? 包含a? %a%? ? ?

? ? ? ? 倒數(shù)第三個(gè)字符是a? %a__? ? 第二個(gè)字母是a最后字母是b? ? _a%b

? ? ? ? 查詢標(biāo)題title中包含記事本的商品標(biāo)題:

select title from t_item where title like '%記事本%';

查詢標(biāo)題title中不包含記事本的商品標(biāo)題:

select title from t_item where title not like '%記事本%';

9.排序: 如果有條件寫在條件的后面 沒有條件寫在表名的后面

默認(rèn)是升序? desc降序? ? asc升序

查詢員工姓名和工資按照工資降序排序:

select ename,sal from emp order by sal desc;

多字段排序: order by 屬性 desc/asc,屬性 desc/asc...

查詢員工的姓名工資部門編號(hào),按照部門編號(hào)降序如果編號(hào)相同按照工資升序排序

select ename,sal,deptno from emp order by deptno desc,sal asc;

分頁(yè)查詢:limit x,y 第一個(gè)參數(shù)代表跳過的條數(shù),第二參數(shù)代表每頁(yè)的數(shù)量.

每頁(yè)10條,第一頁(yè): limit 0,10? ? ? 每頁(yè)5條第8頁(yè):? limit 5*7,8;

每頁(yè)10條,第三頁(yè): limit 20,10? ? ? 每頁(yè)12條第3頁(yè): limit 2*12,12

limit 關(guān)鍵字通常寫在sql語(yǔ)句的最后面

查詢工資前三名的員工姓名和工資

select ename,sal from emp order by sal desc limit 0,3;

10.數(shù)值計(jì)算 + - * /? 5%3等效mod(5,3)

查詢所有的員工姓名,工資及年終獎(jiǎng)(工資*5)

select ename,sal,sal*5 年終獎(jiǎng) from emp;

11.ifnull(x,y)函數(shù)

age=ifnull(x,y)如果x的值為null則賦值y,如果不為null,則賦值x

將emp表中獎(jiǎng)金為null的全部改為0

update emp set comm = ifnull(comm,0);

12.聚合函數(shù) 對(duì)多行數(shù)據(jù)進(jìn)行統(tǒng)計(jì)

求和: sum(求和的字段名)?

查詢所有員工的工資總和

select sum(sal) from emp

平均值: avg(字段名)

查詢10號(hào)部門的平均工資

select avg(sal) from emp where deptno=10;

最大值: max(字段名)

查詢30號(hào)部門的最高工資

select max(sal) from emp where deptno=30;

最小值: min(字段名)

查詢dell商品中最便宜的商品價(jià)格

select min(price) from t_item where title like '%dell%';

統(tǒng)計(jì)數(shù)量:count(字段名/*)

查詢工資大于等于3000的員工數(shù)量

select count(*) from emp where sal>=3000;

13.日期相關(guān)函數(shù)

select 函數(shù)? ,可以執(zhí)行函數(shù)

獲取當(dāng)前的年月日時(shí)分秒? now()? select now();

獲取當(dāng)前年月日 curdate()

獲取當(dāng)前時(shí)分秒 curtime()

提取時(shí)間分量 年 月 日 時(shí) 分 秒

select extract(year from now());

select extract(month from now());

select extract(day from now());

select extract(hour from now());

select extract(minute from now());

select extract(second from now());

? ????????????????????????????????????????????????????????????????????????????????????????? 日期格式化函數(shù)

(標(biāo)準(zhǔn)格式轉(zhuǎn)為非標(biāo)準(zhǔn)格式):date_format(日期,format)?

format:? %Y四位年? %y兩位年

? %m兩位月? %c一位月

? %d日

? %H 24小時(shí)? %h 小時(shí)

? %i 分? ? ? %s秒

把now()格式改成? 年月日時(shí)分秒

select date_format(now(),'%Y年%m月%d日%H時(shí)%i分%s秒');

(非標(biāo)準(zhǔn)格式轉(zhuǎn)為標(biāo)準(zhǔn)格式):str_to_date(非標(biāo)準(zhǔn)時(shí)間,fromat)

? ? ? ? 14.08.2008 08:00:00轉(zhuǎn)為標(biāo)準(zhǔn)格式

select str_to_date('14.08.2018 08:00:00','%d.%m.%Y %H:%i:%s');

14.字符串相關(guān)函數(shù)

? ? 1.字符串拼接 concat(s1,s2);

查詢員工姓名和工資,要求工資以元為單位

select ename,concat(sal,'元') from emp;?

? ? 2.獲取字符串的長(zhǎng)度 char_length(str)

查詢員工姓名和名字的長(zhǎng)度

select ename,char_length(ename) from emp;

? ? 3.獲取字符串在另一個(gè)字符串出現(xiàn)的位置 instr(str,substr) || locate(substr,str)

? ? 4.插入字符串 insert(str,start,length,newstr)

select insert('sdgjhashdaj',2,3,'xiao');

結(jié)果:sxiaohashdaj

? ? 5.去空白 trim(str)

? ? 6.截取字符串 left(str,start)從左邊截? rigth(str,start)從右邊截

? ? ? ? ? ? ? ? ? substring(str,start,length)

? ? 7.重復(fù)repeat(str,count)

select repeat('ab',2);

結(jié)果: abab

? ? 8.replace(str,odl,new)

? ? 9.反轉(zhuǎn) reverse(str)

select reverse('abc');

結(jié)果: cba

15.數(shù)學(xué)相關(guān)函數(shù)

? ? 1.向下取整? ? floor(num)

select floor(3.58);

結(jié)果:3

? ? 2.四舍五入? round(num)

select round(2.33);

結(jié)果:2

? ? ? 四舍五入? ? round(num,m)? m代表小數(shù)位數(shù)

select round(23.889,2);

結(jié)果:23.89

? ? 3.非四舍五入? truncate(num,m)? m代表小數(shù)位數(shù)

select truncate(23.889,2);

結(jié)果:23.88

? ? 4.隨機(jī)數(shù)? rand 0-1的隨機(jī)數(shù)

select floor(rand()*9);

結(jié)果:8

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容