Mysql學習02

語言目前總結:

表級子查詢

  • 分組:group by
  • 平均價格:
select round(avg(price),2) from goods;
  • 查詢所有價格大於平均價格的商品,併案價格降序排序:
select * from goods where price >(select round(avg(price),2)from goods) order by price desc;
  • 查詢類型name 爲‘超極本’的商品名稱,價格:
select name,price from goods where cate_name ='超極本'
- 顯示商品的種類(distinct是區分的不重複內容):select distinct cate_name from goods;
  • 顯示商品種類方式2:
select cate_name from goods group by cate_name;
  • 顯示每種類型的商品的平均價格:
select cate_name, max(price) as 最貴, min(price) as 最便宜,avg(price) as 平均價, count(*) as 總數量 
avg(price) from goods 
group by cate_name;
  • 查詢每種類型中最貴的商品:
select *
from goods inner join
(select cate_name, max(price) as 最貴, min(price) as 最便宜,avg(price) as 平均價, count(*) as 總數量 
avg(price) from goods 
group by cate_name) as info on goodscate_name = info.cate_name and goods.price =info.最貴;

行級子查詢

= => in or => any

select * from goods where (cate_name,price) = in (select cate_name ,max(price) from goods group by cate_name);

創建商品分類表

 create table if not exists goods_cates(id int unsigned primary key auto_increment, name varchar(40) not null);
  • 查詢商品類別
 insert into goods_cates(name) select distinct cate_name from goods;
  • 將good表的brand_name轉化 select distinct cate_name from goods;
update tabel select colum =xx;
update goods as g inner join goods_cates as c on g.cate_name = c.name set g.cate_name = c.id;
  • 更新字段名稱和數據類型
alter table goods change cate_name cate_id int unsigned not null;

待updat 10.30

發佈了44 篇原創文章 · 獲贊 5 · 訪問量 1964
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章