【一周入门MySQL—5】
生活随笔
收集整理的這篇文章主要介紹了
【一周入门MySQL—5】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據庫實例應用
【電商數據處理案例】
目標需求:將某電商脫敏后數據導入數據庫進行加工處理,使用加工好的數據分析業務問題數據獲取
- 客戶相關:UserInfo.csv:用戶主表、RegionInfo.csv:區域表、UserAddress.csv:用戶地址表
- 商品相關:GoodsInfo.csv:商品主表、GoodsBrand.csv:商品品牌表、GoodsColor.csv:商品顏色表、GoodsSize.csv:商品尺碼
- 訂單相關文件:OrderInfo.csv:訂單主表、OrderDetail.csv:訂單詳情表
SQL數據處理:
數據清洗→數據篩選→數據透視→數據排序
表結構一覽:
表之間的關聯關系:
數據準備:
新建數據庫
create database ds;use ds;建表并導入數據
--? UserInfo tablecreate table userinfo(userid varchar(6) not null default '-',username varchar(20) not null default '-',userpassword varchar(100) not null default '-',???sex int not null default 0,usermoney int not null default 0,frozenmoney int not null default 0,addressid varchar(20) not null default '-',regtime varchar(20) not null default '-',lastlogin varchar(20) not null default '-',lasttime date not null);#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/UserInfo.csv"into table userinfofields terminated by ','ignore 1 lines;-- regioninfocreate table regioninfo(regionid varchar(4) not null default '-',parentid varchar(4) not null default '-',regionname varchar(20) not null default '-',???regiontype int not null default 0,agencyid int not null default 0,pt varchar(11) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/RegionInfo.csv"into table regioninfofields terminated by ','ignore 1 lines;-- UserAddresscreate table useraddress(addressid varchar(5) not null default '-',userid varchar(6) not null default '-',??consignee varchar(50) not null default '-',country varchar(1) not null default '-',province varchar(2) not null default '-',city varchar(4) not null default '-',district varchar(4) not null default '-',?address varchar(200) not null default '-',pt varchar(11) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/UserAddress.csv"into table useraddressfields terminated by ','ignore 1 lines;-- GoodsInfocreate table goodsinfo(goodsid varchar(6) not null default '-',typeid varchar(3) not null default '-',markid varchar(4) not null default '-',goodstag varchar(100) not null default '-',brandtag varchar(100) not null default '-',customtag varchar(100) not null default '-',goodsname varchar(100) not null default '-',clickcount int not null default 0,clickcr int not null default 0,goodsnumber int not null default 0,goodsweight int not null default 0,marketprice double not null default 0,shopprice double not null default 0,addtime varchar(20) not null default 0,isonsale int not null default 0,sales int not null default 0,realsales int not null default 0,extraprice double not null default 0,goodsno varchar(10) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/goodsinfo.csv"into table goodsinfofields terminated by ','ignore 1 lines;-- GoodsBrandcreate table goodsbrand(SupplierID varchar(4) not null default '-',BrandType varchar(100) not null default '-',pt varchar(11) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/GoodsBrand.csv"into table goodsbrandfields terminated by ','ignore 1 lines;-- GoodsColorcreate table goodscolor(ColorID varchar(4) not null default '-',ColorNote varchar(20) not null default '-',ColorSort int not null default 0,???pt varchar(11) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/GoodsColor.csv"into table goodscolorfields terminated by ','ignore 1 lines;-- GoodsSizecreate table goodssize(SizeID varchar(4) not null default '-',SizeNote varchar(100) not null default '-',SizeSort int not null default 0,???pt varchar(11) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/GoodsSize.csv"into table goodssizefields terminated by ','ignore 1 lines;-- OrderInfocreate table OrderInfo(OrderID varchar(6) not null default '-',UserID varchar(10) not null default '-',OrderState int not null default 0,PayState int not null default 0,AllotStatus int not null default 0,Consignee varchar(100) not null default '-',Country int not null default 0,Province int not null default 0,City int not null default 0,District int not null default 0,Address varchar(100) not null default '-',GoodsAmount double not null default 0,OrderAmount double not null default 0,ShippingFee int not null default 0,RealShippingFee int not null default 0,PayTool int not null default 0,IsBalancePay int not null default 0,BalancePay double not null default 0,OtherPay double not null default 0,PayTime varchar(20),AddTime varchar(20) not null default '-');#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/orderinfo.csv"into table OrderInfofields terminated by ','ignore 1 lines;-- OrderDetailcreate table OrderDetail(RecID varchar(7) not null default '-',OrderID varchar(6) not null default '-',UserID varchar(6) not null default '-',SpecialID varchar(6) not null default '-',GoodsID varchar(6) not null default '-',GoodsPrice double not null default 0,ColorID varchar(4) not null default '-',SizeID varchar(4) not null default '-',Amount int not null default 0);#導入數據load data infile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/OrderDetail.csv"into table OrderDetailfields terminated by ','ignore 1 lines;-- 查詢導入表的行數select count(*) from userinfo; -- 1000select count(*) from RegionInfo; -- 3415select count(*) from useraddress; -- 10000select count(*) from goodsinfo; -- 10000select count(*) from goodsbrand; -- 64select count(*) from goodscolor; -- 2641select count(*) from goodssize; -- 289select count(*) from orderinfo; -- 3711select count(*) from orderdetail; -- 10000實踐題
表數據調整
用戶信息表
select * from userinfo;-- 時間戳轉換為標準的日期時間型格式set sql_safe_updates=0; -- 設置數據庫的安全權限update userinfo set regtime = from_unixtime(regtime);-- 執行報錯 :Error Code: 1406. Data too long for column 'regtime'-- 因為日期時間型不能直接放到文本格式內alter table userinfo modify regtime datetime; -- 表中列有數據的情況下不能直接修改數據類型-- 在表中添加一個新字段“regtime_new”,類型為日期時間型alter table userinfo add regtime_new datetime;-- 這個時候可以去更新新字段“regtime_new”的值,將時間戳轉換為標準日期時間格式update userinfo set regtime_new = from_unixtime(regtime);-- 同樣的方法,設置lastlogin(最后登錄時間)alter table userinfo add lastlogin_new datetime;update userinfo set lastlogin_new = from_unixtime(lastlogin);區域信息表
select * from RegionInfo;-- 需要將“pt”字段由文本型轉換為日期型-- 首先需要提取文本中的日期信息(中間8個字符)-- 然后將提取的信息轉換為日期型格式(同樣使用添加一列的方式)alter table RegionInfo add pt_new date;update RegionInfo set pt_new=mid(pt,2,8);其他表的調整
select * from useraddress;alter table useraddress add pt_new date;update useraddress set pt_new=mid(pt,2,8);select * from goodsinfo;alter table goodsinfo add addtime_new datetime;update goodsinfo set addtime_new = from_unixtime(addtime);select * from goodsbrand;alter table goodsbrand add pt_new date;update goodsbrand set pt_new=mid(pt,2,8);select * from goodscolor;alter table goodscolor add pt_new date;update goodscolor set pt_new=mid(pt,2,8);select * from goodssize;alter table goodssize add pt_new date;update goodssize set pt_new=mid(pt,2,8);select * from orderinfo;alter table orderinfo add addtime_new datetime;update orderinfo set addtime_new = from_unixtime(addtime);-- paytime字段是支付時間,0的數據不能直接通過from_unixtime()轉換(否則會變成1970-01-01 00:00:00),需要先轉換成nullalter table orderinfo add paytime_new datetime;update orderinfo set paytime_new = from_unixtime(paytime) where paytime <> '0';;select * from orderdetail;-- orderdetail表不需要調整列數據字段格式【實際查詢案例】
-- 1、不同時段的登陸用戶數
-- 按照時間分組,統計每個小時有多少用戶登錄-- 針對字段“lastlogin_new”提取小時select hour(lastlogin_new) 時段,count(userid) 登錄用戶數from userinfogroup by hour(lastlogin_new)order by 時段;-- 在實際情況中最后登錄時間會是不同的日期,我們就可以用來統計最近7天或者30天登錄的用戶數,用來統計潛在流失用戶-- 2、不同時段的下單數量
-- 在訂單信息表orderinfo中,orderid是唯一的select hour(addtime_new) 時段,count(orderid) 下單數量from orderinfogroup by hour(addtime_new)order by 時段;-- 不同時段的累計下單數量select hour(addtime_new) 時段,count(orderid) 下單數量,sum(count(orderid)) over( order by hour(addtime_new) )累計下單數量from orderinfogroup by hour(addtime_new);-- 上面的例子指定了分區內的排序, 默認就是統計滑動窗口第一行到當前行的訂單數量-- 3、當日GMV(未付款訂單金額0+待發貨訂單金額1+已發貨訂單金額2+已取消訂單金額3)
-- GMV(Gross Merchandise Volume)即商品交易總額,是一段時間內的成交總額-- 按照訂單狀態進行orderstate分組select orderstate,sum(orderamount) 訂單金額from orderinfogroup by orderstatewith rollup; -- with rollup 對分組之后的聚合值進行求和-- 4、各省市消費金額(orderinfo-orderamount)
-- orderinfo表中的省份字段province 城市字段city-- RegionInfo表中都存放在regionid字段-- select * from RegionInfo;select r1.regionname 省份,r2.regionname 城市,round(sum(orderamount),2)?? 消費金額from orderinfojoin RegionInfo as r1 on province = r1.regionidjoin RegionInfo as r2 on city = r2.regionidgroup by province,cityorder by province,city;-- 5、不同支付方式的訂單量
-- 支付工具 paytool 用戶提交訂單后會進入支付界面,選擇支付方式,不管支付成功與否,都會產生支付方式select paytool 支付方式,count(orderid) 訂單量from orderinfogroup by paytoolorder by paytool;-- 6、哪種支付方式可能導致用戶支付不成功而取消訂單
-- 查詢因為支付不成功而取消的訂單數量-- 訂單狀態 orderstate = 3 支付失敗 paystate = 0select t1.paytool, 未支付取消的訂單量,每個支付工具訂單總量,ifnull(未支付取消的訂單量/每個支付工具訂單總量,0) 占比from(select paytool,count(orderid) 每個支付工具訂單總量from orderinfogroup by paytool) t1join(select paytool,count(orderid) 未支付取消的訂單量from orderinfowhere orderstate = '3' and paystate = '0'group by paytool) t2on t1.paytool = t2.paytool;-- 7、當日不同品牌的總銷量
-- 銷量在表orderdetail?? 品牌在表goodsbrand-- 通過中間表商品表goodsinfo進行三表連接-- 會存在有的商品品牌沒有在品牌表中存在的情況select goodsinfo.typeid 品牌ID,brandtype 品牌名稱,sum(amount) 銷量from orderdetailleft join goodsinfo on orderdetail.goodsid = goodsinfo.goodsidleft join goodsbrand on goodsinfo.typeid = goodsbrand.supplieridgroup by goodsinfo.typeid;-- 8、當日不同品牌的復購用戶數
select? t.品牌ID,brandtype,count(t.userid) 復購用戶數from(select goodsinfo.typeid 品牌ID,brandtype,userid,count(distinct orderid) 購買次數from orderdetailleft join goodsinfo on orderdetail.goodsid = goodsinfo.goodsidleft join goodsbrand on goodsinfo.typeid = goodsbrand.supplieridgroup by goodsinfo.typeid,useridhaving count(distinct orderid)? > 1) tgroup by t.品牌ID;-- 9、查詢結果保存為表,用于后續重復使用
create table pro_amount asselect r1.regionname 省份,r2.regionname 城市,round(sum(orderamount),2)?? 消費金額from orderinfojoin RegionInfo as r1 on province = r1.regionidjoin RegionInfo as r2 on city = r2.regionidgroup by province,cityorder by province,city;select * from pro_amount;-- 10、將查詢結果導出到指定的路徑中
-- 主要需要使用以下路徑 "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"select r1.regionname 省份,r2.regionname 城市,round(sum(orderamount),2)?? 消費金額from orderinfojoin RegionInfo as r1 on province = r1.regionidjoin RegionInfo as r2 on city = r2.regionidgroup by province,cityorder by province,cityinto outfile "C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/pro_amount.csv";?
?
總結
以上是生活随笔為你收集整理的【一周入门MySQL—5】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【一周入门MySQL—4】数据库进阶练习
- 下一篇: linux cmake编译源码,linu