查看網站流量跨境電商平臺推廣
1. MySQL 案例
1.1. 設計數(shù)據(jù)庫
??1、首先根據(jù)相關業(yè)務需求(主要參考輸出輸入條件)規(guī)劃出表的基本結構
??2、根據(jù)業(yè)務規(guī)則進行狀態(tài)字段設計
??3、預估相關表的數(shù)據(jù)量進行容量規(guī)劃
??4、確定主鍵
??5、根據(jù)對相關處理語句的分析對數(shù)據(jù)結構進行相應的變更。
??設計表的時候每個表的功能要獨立,優(yōu)點:結構清晰,操作數(shù)據(jù)庫的時候提高性能
1.2. 實現(xiàn)數(shù)據(jù)庫
??(1)新建user表
??(2)新建order表
??(3)新建product表
??(4)新建category表
1.3. 操作數(shù)據(jù)庫
1.3.1. 插入記錄
insert into 表名(列名1,列名2,列名3……) values (值1,值2,值3……)
1.3.2. 修改表記錄
update 表名 set 字段名=值,字段名=值,字段名=值…… where 條件
1.3.3. 刪除表記錄
delete from 表名 where id=4;
1.3.4. 查詢操作語法
select [distinct]*| 列名,列名 from 表名 [where條件]
??(1)查詢所有商品
select * from category;
??(2)查詢商品名和商品價格
SELECT product_name,product_price FROM product;
??(3)查詢商品名,使用列別名
select product_name as “商品名稱” from product;
??(4)去掉重復值(按照價格)
select distinct(product_price) from product;
??(5)將所有的商品的價格+10進行顯示
select product_name, product_price+10 from product ;
1.3.5. 條件查詢
??(1)查詢商品名稱為"華為pura70"的商品信息
select * from product where product_name='華為pura70';
??(2)查詢商品名稱含有"pura"字的商品信息(模糊查詢)
select * from product where product_name like '%pura%';
??(3)查詢商品id在(1,3)范圍內的所有商品信息
select * from product where product_id in (1,3);
??(4)查詢商品名稱含有"pura”字并且id為2的商品信息
select * from product where product_name like '%pura%'and product_id=2;
??(5)查詢id為1或者3的商品信息
select * from product where product_id=1 or product_id=3;
1.3.67 排序
??(1)查詢所有的商品,按價格進行排序(升序、降序)
select * from product order by product_price asc;
select * from product order by product_price desc;
??(2)查詢名稱査"pura"的商品信息并且按照價格降序排序
select * from product where product_name like '%pura%' order by product_price desc;
1.3.6. 聚合函數(shù)
??(1)獲得所有商品的價格的總和
select sum(product_price) from product;
??(2)獲得所有商品的平均價格
select avg(product_price) from product;
??(3)獲得所有商品的個數(shù)
select count(product_name) from product;
1.3.7. 分組操作
??(1)根據(jù)category_id字段分組
select category_id,count(*) from product group by category_id;
??(2)根據(jù)category_id分組,分組統(tǒng)計每組商品的平均價格,并且平均價格大于200元
select category_id,avg(product_price) from product group by category_id having avg(product_price)>200;