New mysql exercises

 create database ds;

use ds;



-- 建表导数---------------------------------------
--  UserInfo table
create 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
);
    Fields terminated by ','
	INTO Table UserInfo

# Import Data
load data local infile 'E:/data/rawdata/UserInfo.csv'  
	the ignore Lines. 1; 

SELECT * from 10 UserInfo limit; - checking data 
select count (*) userinfo from; - Check the total number of lines 1000 
desc UserInfo; - Check Data Structure 

- Create a new date and time fields assignment 
alter table userinfo add regtime_ datetime not null ; # create datetime fields 

SET SQL_SAFE_UPDATES = 0; # update authority to open the database 

update userinfo set regtime_ = from_unixtime (regtime ); ## to convert the registration time of 24 hour clock 

- create a new date and time fields assigned 

the ALTER the Table UserInfo the Add lastlogin_ datetime not null; 
Update UserInfo the SET lastlogin_ = FROM_UNIXTIME (lastlogin); 

- delete the original wrong date format field 
the ALTER UserInfo drop the Table column regtime; 
the ALTER UserInfo drop the Table column lastlogin; 

UserInfo desc; 

drop UserInfo Table;

-- --------------regioninfo-------
create 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 local infile 'E:/data/rawdata/regioninfo.csv' 
	into table regioninfo
    fields terminated by ','
	ignore 1 lines;

select * from regioninfo;


-- 提取文本型字段的中间八位字符
update regioninfo set pt = substring(pt,1,8);
alter table regioninfo add pt_ date not null

- Create a new date and time fields assignment
update regioninfo set pt_ = date_format(pt,'%Y-%m-%d');  ##转换日期格式
## Conversion Date format 
- wrong date format to delete the original field
alter table regioninfo drop column pt;

desc regioninfo;

-- drop table regioninfo;

-- --------------UserAddress-------   #创建用户地址表
create 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 '-',
# Import Data
);
    VARCHAR Pt (. 11) Not null default '-'

load data local infile 'E:/data/rawdata/UserAddress.csv' 
	into table useraddress
    fields terminated by ','
	ignore 1 lines;
    
select * from useraddress;
    
update useraddress set pt = substring(pt,1,8);

alter table useraddress add pt_ date not null;
update useraddress set pt_ = date_format(pt,'%y-%m-%d');


select length(pt_) from useraddress;    ##计算该字段的长度

alter table useraddress drop column pt;

select count(*) from useraddress;

select * from useraddress limit 10;

-- drop table useraddress;






-- ----GoodsInfo----    #商品主表
create 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 local infile 'E:/data/rawdata/GoodsInfo.csv' 
	into table goodsinfo
    fields terminated by ','
	ignore 1 lines;

select * from goodsinfo limit 10;

alter table goodsinfo add addtime_ datetime not null;
update goodsinfo set addtime_ = from_unixtime(addtime);

alter table goodsinfo drop column addtime;


-- drop table goodsinfo;

-- ----GoodsBrand----    #商品品牌表
create table goodsbrand(
	SupplierID varchar(4) not null default '-',
	BrandType varchar(100) not null default '-',
	pt varchar(11) not null default '-'
);

#导入数据
load data local infile 'E:/data/rawdata/GoodsBrand.csv' 
	into table goodsbrand
    fields terminated by ','
	ignore 1 lines;

select * from goodsbrand limit 10;

update goodsbrand set pt = substring(pt,1,8);

alter table goodsbrand add pt_ date not null;
update goodsbrand set pt_ = date_format(pt,'%y-%m-%d');

alter table goodsbrand drop column pt;

-- drop table goodsbrand;

-- ----GoodsColor----      ###商品颜色表
create 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 local infile 'E:/data/rawdata/goodscolor.csv' 
	into table goodscolor
    fields terminated by ','
	ignore 1 lines;

select * from goodscolor limit 10;

update goodscolor set pt = substring(pt,1,8);

alter table goodscolor add pt_ date not null;
update goodscolor set pt_ = date_format(pt,'%y-%m-%d');

alter table goodscolor drop column pt;

-- drop table goodscolor;

-- ----GoodsSize----    ##商品尺码表
create 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 local infile 'E:/data/rawdata/goodssize.csv' 
	into table goodssize
    fields terminated by ','
	ignore 1 lines;

select * from goodssize limit 10;

update goodssize set pt = substring(pt,1,8);

alter table goodssize add pt_ date not null;
update goodssize set pt_ = date_format(pt,'%y-%m-%d');

alter table goodssize drop column pt;

-- drop table goodssize;

-- ----OrderInfo----     ###订单主表
create 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) not null default '-',
    AddTime varchar(20) not null default '-'
);

#导入数据
load data local infile 'E:/data/rawdata/OrderInfo.csv' 
	into table OrderInfo
    fields terminated by ','
	ignore 1 lines;

select * from OrderInfo limit 10;
alter table OrderInfo add PayTime_ datetime not null;
update OrderInfo set PayTime_ = from_unixtime(PayTime);



alter table OrderInfo add AddTime_ datetime not null;
update OrderInfo set AddTime_ = from_unixtime(AddTime);


alter table OrderInfo drop column PayTime;
alter table OrderInfo drop column AddTime;

-- drop table OrderInfo;

-- ----OrderDetail----     ###订单详情表
create 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 local infile 'E:/data/rawdata/OrderDetail.csv' 
	into table OrderDetail
    fields terminated by ','
	ignore 1 lines;
    
select * from OrderDetail limit 10;

select count(*) from OrderDetail;

-- drop table orderdetail;


-- 查询导入表的行数
select count(*) from userinfo; -- 1000
select count(*) from RegionInfo; -- 3415
select count(*) from useraddress; -- 10000
select count(*) from goodsinfo; -- 10000
COUNT the SELECT (*) from goodsbrand; - 64- 
select count(*) from goodscolor; -- 2641
select count(*) from goodssize; -- 289
the SELECT COUNT (*) from orderInfo; - 3711 
the SELECT COUNT (*) from OrderDetail; - 10000 

- 1. check your order total amount and the total number of orders for 
select sum (orderamount) , COUNT (OrderID) 
from orderInfo; 

- 2, query order amount more than 3000 yuan order information: order id, user id, the amount of 
the SELECT orderid, userid, orderamount 
from orderInfo 
the WHERE orderamount> 3000; 


- 3, the average amount of consumption inquiry more than 10,000 customers 
the SELECT AVG (orderAmount), UserInfo *. 
from orderInfo left the Join UserInfo ON orderinfo.UserID = userinfo.userid 
Group by userinfo.userid 
the HAVING AVG (orderamount)> 10000; 

- 4. queries up to the amount of consumption of pre-orders ten customers last login time 
select orderinfo.userid, lastlogin_, sum (orderamount ) orders the amount of consumption 
	from orderInfo left the Join UserInfo ON orderinfo.UserID = userinfo.userid 
	Group by orderinfo.userid 
	the Order by the consumer orders amount desc 
    limit 10;
     
- 5, where the city check your order total amount of consumption of the top ten largest customers 

    
## to replace the city can change the city or province of the country such as the query 
select o.userid, sum (orderamount) orders the amount of consumption, regionname city 
	from orderInfo O left the Join RegionInfo r ON o.city = r.regionid 
	Group by o.userid 
	the Order by Order desc amount of consumption 
    10 limit; 
        
-. 6, the strength of the strongest query before ten cities 
select sum (orderamount) strength, regionname city 
from the Join RegionInfo left orderInfo a = B ON a.city b.regionid 
Group by city 
Order by the strength desc 
limit 10; 


--7, query the strongest purchasing power of the top ten cities and provinces where they get # purchasing power by the provinces under a single fee ,,, ranked # 
first check the strongest purchasing power of the top ten cities 
## cities and provinces are due in table regioninfo in a table ,, so self-join query 


select s.regionname provinces, r.regionname city, sum (orderamount) Order total amount of consumption 
	from orderInfo O 
    left the Join regioninfo r oN o.city = r.regionid
    S ON s.regionid the Join RegionInfo left = o.province 
	Group by o.city 
	the Order by the total amount of orders for consumer desc 
    limit 10; 


- 8, total sales and total sales of the product inquiry 

select sum (amount) of total sales, sum ( goodsprice * amount) of total sales 
from OrderDetail; 



- 9, the ten best-selling merchandise inquiry 


select sum (amount) sales, goodsid 
from OrderDetail 
Group by goodsid 
the Order by sales desc 
limit 10; 



- 10, queries ten best-selling brand 


select sum (amount) sales, brandtype 
from OrderDetail O 
left the Join goodsinfo G = ON o.goodsid g.goodsid  
left the Join goodsbrand C = ON g.typeid c.supplierid
Group typeid by 
the Order by sales desc 
limit 10; 


use ds; 
- 11, queries ten best-selling item belongs brand sales of all items 
# queries ten best-selling brand merchandise belongs

DISTINCT e.typeid from SELECT ( 
SELECT a.goodsid, the typeid, SUM (AMOUNT) 
from the Join goodsinfo left OrderDetail A = B ON a.goodsid b.goodsid 
Group by a.goodsid 
Order by SUM (AMOUNT) desc 
limit 10) E; 



c.goodsid SELECT, SUM (AMOUNT * goodsprice) 
from OrderDetail C 
left the Join goodsinfo D = ON c.goodsid d.goodsid 
WHERE d.typeid in 
(SELECT DISTINCT e.typeid 
from (SELECT a.GoodsID, the typeid, SUM (the Amount) 
a OrderDetail from 
left ON a.GoodsID the Join goodsinfo B = b.goodsid 
Group by a.GoodsID 
- 12 is, different kinds of goods received later query the top ten most used by the customer ## cities are what goods because the goods corresponding to a plurality of cities, in a row, so use GROUP_CONCAT () function receipt combined city line
order by sum(Amount) desc
10 limit) E) 
Group by c.goodsid; 


kind # commodity grouping, to find different kinds bought most of the top ten customers 




the SELECT * 
from A OrderDetail left the Join orderInfo ON a.orderid b = b.orderid left the Join RegionInfo c on b.city = c.regionid; ### each corresponding to a single product order information 

select b.userid, count (distinct goodsid) types of goods, 
	GROUP_CONCAT (DISTINCT RegionName) receipt city 
from OrderDetail a 
	left the Join orderInfo B b.orderid a.orderid = ON 
	left the Join RegionInfo C = ON b.city c.regionid 
	Group by b.userid 
    Order by types of goods desc 
    limit 10;

  

Guess you like

Origin www.cnblogs.com/manjianlei/p/11291297.html