創(chuàng)建database(數(shù)據(jù)庫(kù))create database 數(shù)據(jù)庫(kù)名(一般英文或者拼音)
刪除數(shù)據(jù)庫(kù)
drop database 數(shù)據(jù)庫(kù)名
創(chuàng)建數(shù)據(jù)表---
CREATE TABLE `member` (
? `mid` int(8) NOT NULL AUTO_INCREMENT comment '會(huì)員ID',
? `username` varchar(50) NOT NULL DEFAULT '' comment '用戶名',
? `account` varchar(50) NOT NULL comment '賬號(hào)',
? `pw` varchar(250) NOT NULL comment '密碼',
? PRIMARY KEY (`mid`),
key(`account`)
)ENGINE=InnoDB CHARSET=utf8;
刪除數(shù)據(jù)表
drop table 表名
eg:drop table member
添加表字段:
alter table 表名 add 字段名 字段屬性...
eg:ALTER TABLE aaa ADD info varchar(250) not null default '' comment '明細(xì)'
刪除表字段
alter table 表名 字段名
eg:alter table aaa drop info
修改表字段
alter table 表名 modify 字段名 屬性
eg:alter table aaa modify info int(8)
設(shè)置字符編碼
set names utf8
插入數(shù)據(jù)
insert into 表名(列名1辟躏,列名2…)values (value1,value2…)
eg:insert into member (username,pw) values ('張三','22222')
查詢數(shù)據(jù)----
select 字段名1,字段名2 from 表名
eg:select mid,pw from member
條件查詢
select 字段名 from 表名 where 字段名=條件值
eg:select mid,username from member where pw='11111'
多條件都要滿足則條件之間用and鏈接珍德,如果滿足其一就顯示則用or鏈接
模糊查詢
select 字段名 from 表名 where 字段名 like ‘%匹配值%‘
eg:select * from where username like '張%'
更新數(shù)據(jù)
update 表名 set 列名1=值1,列名2=值2 where 條件
eg:update member set username='alen',age=18 where mid=7
注意:沒(méi)有where條件的時(shí)候講更新全表所有數(shù)據(jù)
刪除數(shù)據(jù)
delete from 表名 where 條件
eg:delete from member where member=6
注意:沒(méi)有where條件的時(shí)候會(huì)刪除整張表折剃,如果有表中有自增字段,那么delect刪除之后哥谷,自增字段的值還是會(huì)被占用。
清楚表數(shù)據(jù)(清楚自增字段)
truncate table 表名
eg:truncate table member
注意:后面不能添加where條件
排序(desc從大到小倒序排列,asc從小到大正序排列)
select * from 表名 where 條件 order by 字段名1 desc,字段名2 desc
eg:select * from member where account='bbb' order by buynum desc
分組
select * from 表名 where 條件 group by 字段名1,字段名2 order by 字段名desc
eg:select count(*),memberType,account from member where mid>1 group by memberType,account order by buynum desc
分頁(yè)查詢條數(shù)(第一條數(shù)據(jù)的起始值是0)
select * from 表名 limit 起始位置,查詢條數(shù)
eg:select * from member limit 0,5
聚合函數(shù)having用法(條件查詢)(一般配合group by 使用)
eg:select count(*),memberType from member group by memberType having count(*)>3
查詢結(jié)果去重
eg:select count(distinct account) from member(account一致則不參與統(tǒng)計(jì))
eg:select sum(distinct buynum) from member(buynum 一致則不參與累加)
max(求最大值) min(求最小值) 函數(shù)
eg:select max(buynum) from member
eg:select min(buynum) from member
mysql 函數(shù)
now():獲取當(dāng)前年-月-日 時(shí):分:秒
unix_timestamp('2017-01-04 15:34:25'):把日期格式的時(shí)間轉(zhuǎn)化成時(shí)間戳
FROM_UNIXTIME('1483515265','%Y-%m-%d %H:%i:%s'):時(shí)間戳轉(zhuǎn)化為日期
DATE_FORMAT(endTm,'%Y-%m-%d'):把我們的日期格式轉(zhuǎn)化成我們想要的時(shí)間格式
注意:%Y表示四位數(shù)的年雕薪,%m表示月,%d表示天晓淀,%H表示小時(shí)所袁,%i標(biāo)示分鐘,%s標(biāo)示秒凶掰,%w,標(biāo)示星期
select DATE_SUB(now(),interval 1 year):獲取一年之前的日期
注意(參數(shù)可以為day[表示天] month[標(biāo)示月] hour[小時(shí)] minute[分鐘] )
數(shù)據(jù)統(tǒng)計(jì)
求每天的數(shù)據(jù)總條數(shù)
eg:SELECT count(*),FROM_UNIXTIME(strTm,'%Y') FROM `member` group by FROM_UNIXTIME(strTm,'%Y-%m-%d')
求每個(gè)月的數(shù)據(jù)總條數(shù)
SELECT count(*),FROM_UNIXTIME(strTm,'%Y') FROM `member` group by FROM_UNIXTIME(strTm,'%Y-%m')
case when 條件篩選(as 后面表示別名【小名】)
當(dāng)....則...否則...
case when 條件 then 值1 else 值2 end
或者
case 條件 when 條件一 then 值1 else 值2
eg:select sum(case when buynum>50 then 1 else 0 end) as num1 from member
eg:select sum(case buynum when buynum>50 then 1 else 0 end) as num1 from member
方法二(類似三目運(yùn)算條件為真取表達(dá)式二燥爷,假取表達(dá)式三):
if(條件,表達(dá)式二,表達(dá)式三)
eg:if(buynum>50,1,0);
多表關(guān)聯(lián)查詢
左關(guān)聯(lián)(左表為主搜右表):
select 表名.字段名1,表名.字段名2 from 表1 left join 表2 on 表1.關(guān)聯(lián)字段=表2.關(guān)聯(lián)字段
eg:select a.userid,a.username,b.addr from user as a left join useraddr as b on a.userid=b.mid
右關(guān)聯(lián)(按右表數(shù)據(jù)為主搜左表):
select 表名1.字段,表2.字段 from 表1 right join 表2 on 表1.關(guān)聯(lián)字段=表2.關(guān)聯(lián)字段
eg:select a.userid,a.username,b.addr from user as a right join useraddr as b on a.userid=b.mid
內(nèi)關(guān)聯(lián)(兩張表共有的部分)
select 表名1.字段,表2.字段 from 表1 inner join 表2 on 表1.關(guān)聯(lián)字段=表2.關(guān)聯(lián)字段
eg:select a.userid,a.username,b.addr from user as a inner join useraddr as b on a.userid=b.mid
全連接(所有數(shù)據(jù))
eg:select a.userid,a.account,b.addr from user as a left join useraddr b on a.userid=b.mid union select a.userid,a.account,b.addr from user as a right join useraddr b on a.userid=b.mid
三張表聯(lián)合查詢
eg:select a.userid,a.account,b.mobile,b.sex,c.addr from user as a left join userinfo as b on a.userid=b.mid left join useraddr as c on b.mid=c.mid
表里添加屬性
添加主鍵(字段里的值必須是唯一并且不能空):
alter table 表名 add primary key (`列名`)
eg:alter table test add primary key (`id`)
添加唯一索引(字段里的值必須是唯一的不能重復(fù)但是可以為空):
alter table 表名 add constraint 索引名 unique (字段名)
eg:alter table test add constraint uk_student_name unique (name);
刪除索引:
alter table 表名 drop index 索引名字
eg:alter table test drop index uk_student_name
添加普通索引(最基本索引沒(méi)有任何限制):
alter table 表名 add index 索引名 (字段名)
eg:alter table test add index in_name (name)
添加全文索引(字段存貯內(nèi)容比較龐大的時(shí)候):
alter table 表名 add fulltext (字段名)
eg:alter table test add fulltext (name)
查看表結(jié)構(gòu):
show create table 表名