待整理
5.DML常用語句:
1.insert:插入記錄
2.update:修改記錄
3.delete:刪除記錄
4.select:查詢記錄
6.insert:
1.語法:
1.insert into 表名 values(值1,值2··· ···);
2.insert into 表名(列名1,列名2···) values(值1,值2···);
3.insert into 表名 查詢語句
2.例句:
1.insert into books(bookname,bookprice) select name,price from libary //將libary中的name,price添加到books中
7.select:
1.語法:
1.select 列名|表達式|*
2.from 表名
3.where 條件語句 //分組前過濾
4.group by 列名|表達式 //分組
5.having 條件語句 //分組後過濾
6.order by 排序 [asc,desc] //排序
7.limit 起點,記錄數 //截取
2.條件子句中可以使用的謂詞
1.>,<,!=,=,<=,>=
2.in
3.not
4.between and:連續的區間
5.is null,is not null:不能使用=null來判斷是否爲空
6.like 模糊查詢
1._ :任意一個字符
2.% :任意多個字符
7.and or:邏輯運行符
3.聚合函數(集合函數):
1.count(列名l*):記數
2.sum() :求和
3.avg() :平均數
4.max() :最大數
5.min() :最小值
4.where與having的區別:where是在分組之前進行過濾,having是在分組之後進行過濾,因此在having子句中可以使用分組之後的結果
5.例句:
1.select distinct bookname,'hello' 哈嘍 from books //查詢不重複的書名並在其後加一列名爲哈嘍,內容爲hello的列
2.select distinct bookname as "書 名",bookid //將列名改爲“書名”顯示,as可以省略,書名中間無空格時" "可以省略
3.select * from `user` where name like '%陽%'; //name中含有“陽”的元素,‘--陽’代表name中爲XX陽的元素
4.select SUM(salary),count(*),avg(salary),count(DISTINCT deptnu) 'sort of deptnu' from `user`
5.select u.deptnu,d.dname,SUM(salary),count(*) from `user` u,dept d where u.deptnu=d.deptnu group by u.deptnu //分組必須和聚合搭配使用;多條語句分組,將多個列元素都相同的分爲一組,列名之間用逗號隔開;使用分組時只能select聚合函數,select列則報錯(mysql中不報錯,但查得數據是錯誤的)
6.select deptnu,SUM(salary),count(*) from `user` group by deptnu having COUNT(*)>2
7.select * from `user` order by salary,deptnu //先按照salary,再按照deptnu從小到大排序
8.select * from `user` order by salary desc //按照salary從大到小排序
9.select * from `user` order by salary desc,deptnu limit 0,3 //截取結果,從0開始,截取三個
8.update:更新語句
1.語法:update 表名 set 列名1=值,列名2=值 where 條件
2.例句:
1.update `user` set salary=10000,deptnu=4 where name='陳衍舟'
2.update `user` set salary=salary+100 //所有人的salary加100
3.update `user` set name=lower(name) //所有人的名字改爲小寫
9.delete的語法:
delete from 表名 where 條件
高級查詢:
1.表連接:
1.連接方式:
1.select *|表達式|列 from table1 t1,table2 t2
where 連接條件
2.標準的sql-99語句
2.笛卡爾積:兩個集合的乘積,即表一的每一條記錄(如10條)與表二的每一條記錄(如5條)都關聯,結果爲50條
3.表的別名:上述的t1就是table1的別名,進行連接條件判斷時,可以通過別名調用列名
4.多表連接:三個表連接一般至少要兩個條件
5.非等值連接:where 列名 between and
6.標準的sql-99
1.語法:
select * from table1
連接方式
table2
on 連接條件
2.分類:
1.內連接:完全依賴於連接條件;inner join
2.外連接:保證其中一邊表的數據一定會被查出來,另一邊沒有則用null填充
1.左外連接:左邊的表一定被查出來;left (outer) join
2.右外連接:右邊···;right [outer] join
3.全外連接:兩邊···;full [outern] join ;mysql不支持
3.自連接:
7.例句:
1.select * from `user`,salaryclass where salary BETWEEN low_salary and high_salary //查詢每條記錄salary對應的等級
2.select * from dept d left outer join `user` u on u.deptnu=d.deptnu //左外連接
2.子查詢(嵌套):
1.在一個查詢語句中包含另一個查詢語句
2.註釋:-- 註釋語句
3.例句:
1.select * from `user` where deptnu=(select deptnu from dept where dname='開發部') //查找開發部在user中對應的記錄
2.select * from `user` where salary>(select AVG(salary) from `user`) //查找所有大於平均工資的記錄
3.select * from `user` where salary in (select max(salary) from `user` where deptnu=3) //比所有deptnu=3的salary都大的salary
4.select * from `user` where deptnu=1 or deptnu=2
5.select * from `user` where deptnu in|any|all|not in(select deptnu from dept where dname in('開發部','銷售部'))
6.select * from `dept` where deptnu not in(select DISTINCT deptnu from `user`) //查找沒有人的部門
7.select count(*),u.deptnu,dname from `user` u,dept d where u.deptnu=d.deptnu group by u.deptnu having count(*)>(select count(*) from `user` where u.deptnu=1) //查找人數比deptnu=1多的記錄
8.select * from (select deptnu,count(*),avg(salary) sal from `user` group by deptnu) a where a.sal>10000 //查出分組後平均salary大於10000的記錄
4.位置:
1.where:
2.having:
3.from:
4.select:不常用
5.常用謂詞:因爲> <,>=,=,<=只能比較一個唯一值,而不對多個查詢記錄進行比較,因此推出了一些用於集合操作的謂詞
1.all:
2.any:
3.in:
4.not in:
6.exists(存在)|not exist:
1.語法:select * from 表名 where exists(查詢語句)
2.例:
1.select * from tb_class where classid in(select classid from tb_student) //先查裏面
2.select * from tb_class c where exists(select classid from tb_student s where c.classid=s.classid) //先查外面,再查括號裏的內容看結果返回的是true或false,查詢出有學生的班級,外面少用exist更合適,否則用in性能更好
3.函數(自查API):
1.字符函數:
2.數值函數:
3.時間函數:
1.根據出生日期計算年齡:
1.select year(SUBTIME(NOW(),SBIRTH)) age from student
2.select DATEDIFF(NOW(),SBIRTH)) age from student
4.加密:
5.系統函數:
6.case when:實現java的分支
1.(可以完成行變列的查詢)
2.例句:select ACCOUNTID,name,case when remain<1000 then '絲絲' when remain>10000 then'富豪' else '中產階級' end from ACCOUNT