紀(jì)檢監(jiān)察網(wǎng)站建設(shè)牡丹江seo
# ----------排序查詢--------
# 語(yǔ)法
# select 字段列表?from 表名?order by 字段1 排序方式1 ,字段2 排序方式2 ; ?DESC 降序??ASC升序
# 1 根據(jù)年齡對(duì)公司的員工進(jìn)行升序排序---默認(rèn)升序-黃色提示代碼冗余
select * from userTable order by age ASC ;
# 2 根據(jù)入職時(shí)間,對(duì)員工進(jìn)行降序排序
select * from userTable order by entrydate DESC ;
# 3 根據(jù)年齡對(duì)公司的員工進(jìn)行升序排序,年齡相同,在按照入職時(shí)間進(jìn)行降序排序
# (首先對(duì)age進(jìn)行升序排序,-之后對(duì)年齡相同的-用入職時(shí)間進(jìn)行降序排序)
select * from userTable order by age asc ,entrydate DESC ;
# =============================================
# ---------------分頁(yè)查詢--------------
# 字段?limit
# 語(yǔ)法?select 字段列表?form 表?limit 起始索引 查詢記錄數(shù)?;
# **** ?索引公式--(查詢)頁(yè)碼-1 * 頁(yè)面展示數(shù),起始頁(yè)0
# 1 查詢員工第一頁(yè)員工數(shù)據(jù),每頁(yè)展示2條
select * from userTable limit 0 , 2 ;
# 2 查詢員工第二頁(yè)員工數(shù)據(jù),每頁(yè)展示2條?(其實(shí)頁(yè)-》頁(yè)碼-1 * 條數(shù))
select ?* from userTable limit 2 , 2;
案例練習(xí)--dql查詢語(yǔ)句
# 批量添加 ?????????????字段 ??????????????????????????????????????????????????????值
insert into userTable(id, workno, name, gender, age, idcard, worknoaddress, entrydate)
values (1,'1','柳巖','女',18,'123123123123123123','武漢','2023-6-26'),
???????(2,'2','張無(wú)忌','男',20,'123123123123123123','天龍八部','2022-7-26'),
???????(3,'3','韋一笑','男',18,'123123123123123123','天龍八部','2021-7-26'),
???????(4,'4','周芷若','女',25,null,'天龍八部','2023-7-26'),
???????(5,'5','謝遜','男',88,'12312312312312312x','天龍八部','2023-3-26'),
???????(6,'6','張三豐','女',100,'123123123123123123','天龍八部','2023-2-26'),
???????(7,'7','劉亦菲','女',18,'123123123123123123','武漢','2023-7-26')
???????;
# 1 查詢年齡為?20 21 22 23 得男性員工信息
select * from userTable where gender = '男' && (age = 20 || age = 21 || age = 22 ?|| age = 23) ;
select * from userTable where gender = '男' && age in (20,21,22,23);
select * from userTable where gender = '男' && age between 20 and 23 ;
# 2 查詢 性別未男,且年齡再20-40(含)以內(nèi)的姓名未三個(gè)字的員工?(含20 和40)
select * from userTable where gender = '男' && (age between 20 and 40) && name like '---';
select * from userTable where gender = '男' && (age >= 20 && age <= 40) && name like '---';
# 3 統(tǒng)計(jì)員工表,年齡小于60的 ,男性員工和女性員工的人數(shù)
select gender,count(*) from userTable where age < 60 group by gender;
# 4 查詢所有年齡小于等于35歲員工的姓名和年齡,并對(duì)查詢結(jié)果按年齡升序排序,如果年齡相同按入職時(shí)間降序排序
# 5r
select name,age ,entrydate from userTable where age <= 35 order by age asc , entrydate desc ;
# 5 查詢性別為男,年齡10-40(含)以內(nèi)的5個(gè)員工信息,對(duì)查詢結(jié)果按照年齡升序排序,年齡相同按入職時(shí)間升序排序
select * from userTable where gender = '男' && age between 10 and 40 order by age asc ,entrydate desc limit 0 , 5;
DQL ?編寫(xiě)順序 ?執(zhí)行順序
?
?
# 查詢年齡大于15的員工的姓名,年齡,并根據(jù)年齡進(jìn)行升序排序---驗(yàn)證執(zhí)行順序
select t.age tage,t.name tname from userTable t where t.age > 15 order by tage asc;
# 首先 執(zhí)行?from ?表明 別名?t
# 再執(zhí)行?where 后的條件??---- 可以使用t.age ??,執(zhí)行順序在select 前面無(wú)法使用tage別名
# 再執(zhí)行?group by
# 再執(zhí)行分組條件?having
# 再執(zhí)行select --------------字段?t.age 別名tage
# 再執(zhí)行?order by ?執(zhí)行順序在select后面可以使用別名??tage
# 最后?limit
# 總結(jié)
# 字段 和 表 加別名 ?是?as關(guān)鍵字 ?也可以 省略
# where 條件可以使用?and(&&) or(||) 進(jìn)行連接
# 分組查詢??group by 分組???having 對(duì)分組后進(jìn)行過(guò)濾
# 排序查詢???order by ?asc升序??desc降序
# 分頁(yè)??limit 參數(shù) 起始索引(從零開(kāi)始),每頁(yè)展示的數(shù)據(jù) ???查詢頁(yè)碼?= 索引?= ??查詢頁(yè)?- 1 * 條數(shù)
# ==========================================
# --------------------------dcl-------(管理數(shù)據(jù)庫(kù)用戶-控制數(shù)據(jù)庫(kù)的訪問(wèn)權(quán)限)
#用戶管理
# 1查詢用戶
# 用戶權(quán)限都存在?mysql 數(shù)據(jù)庫(kù)的?user表里
# 2創(chuàng)建用戶
# 語(yǔ)法?create user '用戶名'@'主機(jī)名' identified by '密碼'
# 3修改用戶密碼
# 4刪除用戶
# 需求
# 創(chuàng)建用戶itcast 只能夠在當(dāng)前主機(jī)localhost訪問(wèn),密碼123456
create user 'itcast'@'localhost' identified by '123456';
# 創(chuàng)建用戶heima 訪問(wèn)密碼1234 ??%代表任意主機(jī)
create user 'heima'@'%' identified by '123';
# 修改用戶密碼--
# 語(yǔ)法 ?????????????????????????????????????????密碼加密方式
# alter user '用戶名'@'主機(jī)名' identified with mysql_native_password by '新密碼'
alter user 'heima'@'%' identified with mysql_native_password by '1234';
# 刪除itcast@localhost用戶
drop user 'itcast'@'localhost';
# 總結(jié)--主機(jī)名可以使用%通配(任意主機(jī)都可以訪問(wèn))
#運(yùn)維??dba ??使用較多
# ------------dcl--權(quán)限控制
# 查詢權(quán)限-----語(yǔ)法?show grants for '用戶名'@'主機(jī)名'
show grants for 'heima'@'%';
# 通用就--------------------------- ????*.*
# 授予權(quán)限-----語(yǔ)法?grant 權(quán)限列表?on 數(shù)據(jù)庫(kù)名.表名?to '用戶名'@'主機(jī)名'
# ???所有權(quán)限 ??擁有itcast 所有表 ??用戶??????---從右向左讀
grant all on itcast.* to 'heima'@'%';
# 所有權(quán)限 ?擁有所有數(shù)據(jù)庫(kù) 所有表 ?用戶??---從右向左讀
grant all on *.* to 'heima'@'%';
# 撤銷(xiāo)權(quán)限-----語(yǔ)法revoke 權(quán)限列表?no 數(shù)據(jù)庫(kù)名.表名?from '用戶名'@'主機(jī)名'
revoke all on itcast.* from 'heima'@'%';
?
?
上圖為常用---還有其他的可以查看官方文檔
權(quán)限總結(jié)
?