日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Mysql高级操作

發(fā)布時間:2024/10/6 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql高级操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一.新增數(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高级操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。