MySQL子查詢與鏈接

 

 

 

 

 

 

 

 

 

——MySQL子查詢與鏈接
一、set names gbk: 設置客戶端的顯示形式爲gbk(若是以前設置是utf-8編碼則顯示出來文字不能識別)
二、子查詢指嵌套在查詢內部,必須在圓括號裏使用,返回值是一個標量、一行也開始是一個數據表
三、子查詢是指在另外一個查詢裏面的select子句
四、在進行數據表的寫入時,默認是不容許寫入漢字的,所以須要修改一下編碼格式:
show full columns from phplamp; 檢查數據表全部字段的狀態
alter table phplamp change address address varchar(100) character set utf8 collate utf8_unicode_ci not null; 修改成utf8編碼格式
set names gbk;將客戶端修改成gbk來顯示數據字段
五、子查詢語句:
select avg(goods_price)from tdb_goods;求取某一列的平均值
select round(avg(goods_price),2) from tdb_goods;求取某一列的平均值,保留兩位小數
select goods_id,goods_name,goods_price from tdb_goods where goods_price>=5636.36;查找價格大於平均值的商品的id,name和price
select goods_id,goods_name,goods_price from tdb_goods where goods_price>=(select round(avg(goods_price),2) from tdb_goods);子查詢方式
select goods_price from tdb_goods where goods_cate="超級本"; 選擇輸出超極本筆記本的價格
select * from tdb_goods where goods_cate="超級本"\G;列顯示超極本筆記本的基本信息
六、一些子查詢關鍵字any/some/all/not in/in/
select goods_id,goods_name,goods_price from tdb_goods where goods_price>=any(select goods_price from tdb_goods where goods_cate="超級本");若是子查詢返回並不是一列多個值時須要添加any,all,some
select goods_id,goods_name,goods_price from tdb_goods where goods_price=any/some(select goods_price from tdb_goods where goods_cate="超級本");大於等於,小於等於可使用any和all,可是等於只能夠用some/any
select goods_id,goods_name,goods_price from tdb_goods where goods_price not in(select goods_price from tdb_goods where goods_cate="超級本")\G;not in和!=all是等效的
select goods_cate from tdb_goods group by goods_cate;使用group by函數進行結果的分類統計
insert tdb_goods_cate(cate_name) select goods_cate from tdb_goods group by goods_cate; insert-select語句查詢
七、多表更新函數
多步更新:
update tdb_goods inner join tdb_goods_cate on goods_cate=cate_name set goods_cate =cate_id; 內鏈接更新方式,多步更新—創鍵表-經過insert-select寫入記錄-表的更新
一步更新方式:crested select函數:
create table tdb_goods_band(
-> band_id smallint unsigned primary key auto_increment,
-> brand_name varchar(50)character set utf8 collate utf8_unicode_ci not null) 編碼方式爲utf8漢字可輸入型
-> select brand_name from tdb_goods group by brand_name;
更新結束以後咱們須要將數據列的名稱和屬性一塊兒修改:
alter table tdb_goods
-> change goods_cate cate_id smallint unsigned not null,
-> change brand_name brand_id smallint unsigned not null;
將字符型修改成數字型,縮小佔用空間
八、多表鏈接
內鏈接:只顯示兩個表符合鏈接條件的記錄
select goods_id,goods_name,cate_name from tdb_goods inner join tdb_goods_cate on tdb_goods.cate_id=tdb_goods_cate.cate_id;
左外鏈接:顯示左邊所有記錄以及右邊符合條件的記錄
select goods_id,goods_name,cate_name from tdb_goods left join tdb_goods_cate on tdb_goods.cate_id=tdb_goods_cate.cate_id;
右外鏈接:鏈接右表的所有記錄以及左表符合條件的記錄
select goods_id,goods_name,cate_name from tdb_goods right join tdb_goods_cate on tdb_goods.cate_id=tdb_goods_cate.cate_id;
多個表之間的鏈接(和兩個錶鏈接是類似的)
select goods_id,goods_name,cate_name,brand_name,goods_price from tdb_goods as g inner join tdb_goods_cate as c on g.cate_id=c.cate_id inner join tdb_goods_band as b on g.brand_id=b.band_id;
九、無線級分類表的設計(子結構鏈接)
CREATE TABLE tdb_goods_types(
type_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
type_name VARCHAR(20) character set utf8 collate utf8_unicode_ci not null,
parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0
);
自身鏈接方式-相互嵌套
select s.type_id,s.type_name,count(p.type_name) from tdb_goods_types as s left join tdb_goods_types as p on s.parent_id=p.type_id;左表爲子表
select p.type_id,p.type_name,count(s.type_name) from tdb_goods_types as p left join tdb_goods_types as s on s.parent_id=p.type_id group by p.type_name order by p.type_id;右表爲父表
十、多表刪除
(1)記錄重複合併操做:
select goods_id,goods_name from tdb_goods group by goods_name; 以goods_name做爲重複合併的字段
(2)多表刪除-對照刪除
delete t1 from tdb_goods as t1 left join (select goods_id,goods_name from tdb_goods group by goods_name having count(goods_name)>=2) as t2 on t1.goods_name=t2.goods_name where t1.goods_id>t2.goods_id;php

相關文章
相關標籤/搜索