Mysql高级操作
一.新增數(shù)據(jù)
????????? 1.基本語法:insert into 表名(字段列表) values(值列表);
?????????? 2.主鍵沖突:可以(duplicate)進行選擇性處理:更新或替換
??????????????????? 1):更新操作: insert into 表名(字段列表:包含主鍵) values(值列表) on duplicate key update 字段=值;
???????????????????? ? ? ? insert into test3 values(1,'steven','female') on? duplicate key update sex='female';
??????????????????? 2):替換操作:replace into 表名(字段列表:包含主鍵) values(值列表 );
?????????????????????????? replace into test3 values(1,'candy','female');
??????????? 3.蠕蟲復制:
??????????????????? 1):從已有的表中獲取數(shù)據(jù)并將數(shù)據(jù)進行新增操作,從已有表中創(chuàng)建新表(復制表結(jié)構(gòu))
?????????????????????????? create table 表名 like database.table; ????????create table my_copy like test3;
???????????????????? 2):復制表中的數(shù)據(jù)
?????????????????????????? insert into 表名(字段列表) select * from 表名; ????insert into my_copy select * from test3;
???????????????????? 3):蠕蟲復制的意義
??????????????????????????? 1.重已有表中復制數(shù)據(jù)
??????????????????????????? 2.使表膨脹到一定的數(shù)量級,測試表的壓力及效率。
二.更新&刪除數(shù)據(jù)
??????????? 1.基本語法:update 表名 set 字段=值 [where條件][limit 更新數(shù)量];
??????????? ??? update my_copy set name='mary' where name='candy' limit 10;
??????????? 2.基本語法:delete from 表名 [where條件][limit更新數(shù)量];
??????????????? delete from my_copy where id=1 limit 4;
??????????? 3.清空表重置自增長:truncate 表名;
三.查詢語句
??????? ??? 1.基本語法:select * from 表名;
??????????? 2.完整語法:select [select選項]? 字段列表 [別名] from 數(shù)據(jù)源?? [where子句] [group by子句] [having 子句]
????????????? [order by] [limit子句];
??????????? 3.具體參數(shù)解釋
????? ????? ??????? 1.select選項:1):查詢出來的結(jié)果的處理方式 All 默認,保留所有字段??
????????????????????????????????????????? 2): distinct將查詢出來的結(jié)果去掉重復的(所有字段都相同)
?????????????????????????????????????? ? select distinct * from my_copy;
??????????????????? 2.字段別名:多張表聯(lián)合查詢可能會出現(xiàn)重名字段,這時需要起別名進行區(qū)分
????????????????????????????????????????select id 序號,name 姓名,sex 性別 from my_copy;
??????????????????? 3.數(shù)據(jù)源:關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)源都是數(shù)據(jù)表
??????????????????????????????????? 1.多數(shù)據(jù)源查詢? select * from my_class,test3;
??????????????????????????????????? 2.從一張表中取出一條記錄對應另一張表的所有條記錄,全部保留(字段和記錄數(shù)),這種結(jié)果叫笛卡爾積(交叉連接)
??????????????????????????????????? 3.子查詢:一條查詢記錄作為另一條查詢記錄的數(shù)據(jù)源
?????????????????????????????????????? select * from (select * from student) as stu; as指的是數(shù)據(jù)源的別名
??????????????????? 4.where子句:用來判斷篩選數(shù)據(jù),where唯一從磁盤讀取數(shù)據(jù)就開始就行判斷,判斷成功保存到內(nèi)存,失敗就放棄。
??????????????????????????????? ??? 1.判斷條件
?????????????????????????????????????????? 1):比較運算符:>,<,=,!=,>=,<=,like,between and,in,not in
?????????????????????????????????????????? 2):邏輯運算符:&&(and),II(or),!(not)
?????????????????????????????????????????? 3):查找id為1,3,5的學生
?????????????????????????????????????????????? select * from student where id in(1,3,5);
?????????????????????????????????????????????? select * from student where id=1 || id=3 ||id=5;
?????????????????????????????????????????? 4):查找height在某一區(qū)間
?????????????????????????????????????????????? select * from student where height between 109 and 168;
? ? ? ? ?????????????????????????????????????????? select * from student where height>=109 && height<=168 ;????
????????????????????? 5. group by子句:根據(jù)某個字段分組(相同的分一組)?
??????????????????????????????????????????? 1.基本語法:select * from 表名 [where子句]? [group by子句];
??????????????????????????????????????????? 2.分組的意義:為了統(tǒng)計數(shù)據(jù)(按住統(tǒng)計:按分組字段進行數(shù)據(jù)統(tǒng)計)
????????????????????????????????????????? ? 3. Sql提供的統(tǒng)計函數(shù)
???????????????????????????????????????????????????? ? 1):count()統(tǒng)計分組后的記錄數(shù),每組有多少條記錄。
??????????????????????????????????????????????????????????????????? 1.count(*)代表統(tǒng)計所有記錄
??????????????????????????????????????????????????????????????????? 2.count(字段名):代表統(tǒng)計的字段(空字段不統(tǒng)計)
?????????????????????????????????????????????????????? 2):max()統(tǒng)計每組中的最大值
?????????????????????????????????????????????????????? 3):min()統(tǒng)計每組中的最小值
?????????????????????????????????????????????????????? 4):avg()統(tǒng)計每組中的平均值
?????????????????????????????????????????????????????? 5) :sum()統(tǒng)計和
???????????????????????????????????????????? 4.具體應用:
?????????????????????????????????????????????????????? 1)單字段分組:select sex,count(*),max(height),min(height),avg(age),min(age) from student group by sex;
??????????????????????????????????????????????????????? 2)多字段分組:select uid,sex,count(*) from student group by uid,sex;
?????????????????????????? 6.having子句:where是針對磁盤數(shù)據(jù)進行判斷,進入內(nèi)存之后,會進行分組操作,分組操作就需要having,having能使用別名
?????????????????????????????????????????? 1.求出班級所有人數(shù)大于2的學生人數(shù):select uid,name,count(*) from student group by uid having count(*)>=2;
?????????????????????????????????????????? 2.求出班級所有人數(shù)大于2的學生人數(shù)(使用別名):
????????????????????????????????????????? select uid,name,count(*) as total from student group by uid having total>=2;
?????????????????????? 7.order by子句:select * from student group by uid,sex desc;先uid 升序在sex 降序
????????????????????? 8.limit子句:限制數(shù)量
?????????????????????????????????????? 1.只用來限制數(shù)量:select * from student limit 2;
?????????????????????????????????????? 2.限制起始位置,限制數(shù)量:select? * from student 3,2;
總結(jié)
- 上一篇: Mysql列属性
- 下一篇: linux cmake编译源码,linu