本文主要給大家簡(jiǎn)單講講常用的一些MySQL語句及用法,相關(guān)專業(yè)術(shù)語大家可以上網(wǎng)查查或者找一些相關(guān)書籍補(bǔ)充一下,這里就不涉獵了,我們就直奔主題吧,希望常用的一些mysql語句及用法這篇文章可以給大家?guī)硪恍?shí)際幫助。
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的內(nèi)鄉(xiāng)網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1.開啟mysql數(shù)據(jù)庫服務(wù)
格式: net start mysql
如: net start mysql57
2.關(guān)閉mysql數(shù)據(jù)庫服務(wù)
格式: net stop mysql
如: net stop mysql57
3.登陸mysql服務(wù)
格式: mysql -u root -p
如: mysql -u root -p 執(zhí)行成功后,在輸入密碼,
4. 查看當(dāng)前數(shù)據(jù)庫的版本xinxi
格式: select version();
5.查看當(dāng)前時(shí)間
格式: select now();
6.退出,
quit,exit
2.數(shù)據(jù)庫database的操作
1.查看當(dāng)前有哪些數(shù)據(jù)庫
格式: show databases;
2.創(chuàng)建一個(gè)數(shù)據(jù)庫
格式: create database 數(shù)據(jù)庫名 charset=utf8;
例: create database wen charset=utf8;
3.刪除一個(gè)數(shù)據(jù)庫
格式: drop database 數(shù)據(jù)庫名;
例: drop database wen;
4.使用數(shù)據(jù)庫
格式:use 數(shù)據(jù)庫名;
例: use wen;
5.查看當(dāng)前使用數(shù)據(jù)庫
格式: select database();
3.表table的操作
1.查看當(dāng)前數(shù)據(jù)庫有哪些表
格式: show tables;
2.創(chuàng)建一個(gè)表
格式: create table 表名(字段名及類型);
例: create table student(id int auto_increment primary key,name varchar(20) not null,age int not null,address varchar(20),gender bit default 1);
3.查看表的結(jié)構(gòu)
格式: desc 表名;
例: desc student;
4.查看創(chuàng)建表的語句
格式: show create table 表名;
5.刪除表
格式: drop table 表名;
例: drop table student;
6.修改表
1.修改表的名字
格式: rename table 舊表名 to 新表名;
例: rename table student to students;
2.修改表的結(jié)構(gòu), add | drop | change
1.add 添加一個(gè)新的字段
格式: alter table 表名 add 字段名 類型;
例: alter table student add phone int;
2.change 修改
格式: alter table 表名 change 舊字段名 新字段名 新字段類型;
例: alter table student change phone number varchar(20);
3.drop 刪除
格式: alter table 表名 drop 字段名;
例: alter table student drop number;
4.數(shù)據(jù)的操作
1.插入數(shù)據(jù)
1.插入全列數(shù)據(jù)
格式: insert into 表名 values(值1,值2,....)
注意:值得順序需要與表的結(jié)構(gòu)對(duì)應(yīng), 主健可以寫0
例: insert into student values(0,"小明",36,"北京朝陽區(qū)",1);
2.缺省插入
格式:insert into 表名(字段名1,字段名2,....) values(值1,值2,....)
例: insert into student(name,age,address) values("小東",48,"深圳南山區(qū)");
3.插入多條數(shù)據(jù)
格式: insert into 表名 values(值1,值2,....),(值1,值2,....),(值1,值2,..)
例: insert into student values(0,"小云",45,"杭州",0),(0,"小雨",105,"俄羅斯",0),(0,"小清",99,"日本",0);
2.刪除數(shù)據(jù)
格式: delete from 表名 where 條件
例: delete from student where age = 105;
delete from student
不加條件表示全部刪除, 請(qǐng)謹(jǐn)慎使用
3.修改數(shù)據(jù)
格式:update 表名 set 字段名1=值,字段名2=值,... where 條件;
update student set gender = 1 where name = "小青";
update student set age = 52,address="深圳" where name = "小東";
4.查看所有數(shù)據(jù)
格式: select * from 表名;
例: select * from student;
5.查詢數(shù)據(jù)
1.查詢格式
1.查詢所有
格式: select * from 表名;
2.按條件查詢
格式: select 字段名1,字段名2,... from 表名 where 條件;
1.select后跟的是查詢結(jié)果需要顯示的字段名
2. * 表示所有的字段名都要顯示
3. from 后跟的是表名,表示從那個(gè)表查
4. where 后跟的是篩選條件
5. 可以使用 字段名 as 別名 來給字段名取個(gè)別名
例: 顯示 name和age
select name,age from student;
select name as "姓名" from student;
2.條件中的運(yùn)算
1.比較運(yùn)算符
> 大于
< 小于
>= 大于等于
<= 小于等于
= 等于
!= 不等于
格式: select 字段名1,字段名2,... from 表名 where 字段名 比較運(yùn)算符 值;
例: select * from student where age < 50;
2. 邏輯運(yùn)算符
and 且
or 或者
not 非
格式: select 字段名1,字段名2,... from 表名 where 字段名 比較運(yùn)算符 值 邏輯運(yùn)算符 字段名 比較運(yùn)算符 值 ...;
例: select * from student where age < 50 and age > 40;
select * from student where not (age < 50 and age > 40);
3.模糊運(yùn)算符 like
% 表示多個(gè)任意字符
_ 表示一個(gè)任意字符
需求: 匹配所有姓小的人
格式: select * from student where name like "小%";
格式: select * from student where name like "小_";
需求: 匹配名字中有 "小"字的
select * from student where name like "%小%";
4.范圍查詢
in (多個(gè)值 ) 判斷一個(gè)值 是否是多個(gè)值中的一個(gè)
between 值1(包含) and 值2(包含) 判斷一個(gè)值是否在 值1與值2之間
格式: select 字段名1,字段名2,... from 表名 where 字段名 范圍運(yùn)算符 值;
例: 找出 25或者45或者80或者 90
select * from student where age in (25,45,80,90);
需求:找出25 到 45之間的數(shù)據(jù)
select * from student where age between 25 and 45;
5. 空判斷
is null 為空
is not null 不為空
格式: select 字段名1,字段名2,... from 表名 where 字段名 is null;
插入數(shù)據(jù): insert into student(name,age) values("小·超",60);
例: select * from student where address is null;
select * from student where address is not null;
6.去除重復(fù)的值
格式: select distinct 字段名 from 表名 where 條件;
例: select distinct gender from student;
7.聚合函數(shù)
count(*) 求當(dāng)前結(jié)果總共有多少條數(shù)據(jù)
sum(列名) 求列名對(duì)應(yīng)列的 和
avg(列名) 求當(dāng)前列的平均值
max(列名) 求當(dāng)前列的最大值
min(列名) 求當(dāng)前列的最小值
例: 求當(dāng)前表總共有多少條數(shù)據(jù)?
select count(*) from student;
求年齡最小的?
select min(age) from student;
8.分組 group by
格式: select 字段名... from 表名 where 條件 group by 字段名
查看有多少種性別
例: select gender from student group by gender;
需求:統(tǒng)計(jì) 男生 和 女生 各有多少個(gè)
select gender,count(*) from student group by gender;
需求: 統(tǒng)計(jì)所有女生的個(gè)數(shù)?
例: select gender,count(*) from student group by gender having gender = 1;
where 查詢條件, 是先執(zhí)行的查詢條件
having 查詢條件 是在查詢結(jié)果的基礎(chǔ)上在查詢
9. 排序
格式: select 字段名... from 表名 where 條件 order by 字段名1,字段名2...
例: 年齡小到大
select * from student order by age;
默認(rèn)是從小到大排列
asc 從小到大
desc 從大到小
select * from student order by age asc
10. 分頁
格式: select 字段名... from 表名 where 條件 limit 起始值,多少條數(shù)據(jù)
起始值可以從 0 開始
例: select * from student limit 0,3;
常用的一些mysql語句及用法就先給大家講到這里,對(duì)于其它相關(guān)問題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會(huì)捕捉一些行業(yè)新聞及專業(yè)知識(shí)分享給大家的。
當(dāng)前文章:常用的一些mysql語句及用法
網(wǎng)站路徑:http://www.jbt999.com/article32/pspjpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、網(wǎng)頁設(shè)計(jì)公司、App開發(fā)、動(dòng)態(tài)網(wǎng)站、微信公眾號(hào)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)