這篇有點水印机,是我看《sql必知必會》的筆記逢艘,凌亂的一碼~睡毒,而且這本書著重介紹的是SQL語言省容,對于特定的DBMS不夠深入,所以這周我看了同作者寫的《MySQL必知必會》哈哈凡恍。在這里立個FLAG吧吞歼,三天看完壮虫,然后總結(jié)出一篇筆記,也就是周三晚上12點截止奋岁,我會發(fā)布mysql的讀書筆記的思瘟,吼吼。如果我有幸讓大家看到這篇文章厦取,并且想交流一下的話,可以加我微信哦管搪,我好閑的哈哈虾攻。wx:zz1994yr.铡买。
我又回來了~~昨天跟朱老板聊國際內(nèi)外形勢,頗有點書生意氣霎箍,很是酣暢奇钞。1點左右上床睡覺,跟平常比起來算是很早了漂坏,今天5點多就醒了過來景埃,窗外霧蒙蒙的,卻有一層紅色紗裙環(huán)繞著天際顶别,不多會谷徙,紅彤彤的太陽就從山尖尖那里冒出頭了。嗨驯绎,美景總是來的那么不經(jīng)意完慧。
跑偏了哈,今天繼續(xù)Mysql的學(xué)習(xí)咯剩失,來介紹一下基操屈尼。
[if !supportLists]一、[endif]seclect語句查詢
seclect () from ();
seclect () ,() from ();
seclect distinct () from ();
[if !supportLists]二拴孤、[endif]order by語句排序
order by 字句放在最后脾歧,默認(rèn)從小到大,從A到B演熟。
order by ();
order by () , ();
order by () desc ;
[if !supportLists]三鞭执、[endif]where語句過濾
操作符
< ; >; <>; !=; !<; !>; <=; >=;between … and … ; IS NULL;
[if !supportLists]四、[endif]高級數(shù)據(jù)過濾
[if !supportLists]1.????[endif]“ADN””O(jiān)R”來進(jìn)行多組篩選绽媒,AND優(yōu)先級高蚕冬,使用時盡量用小括號明確操作順序。
[if !supportLists]2.????[endif]IN 語句是辕。?IN(— 囤热,—);
[if !supportLists]3.????[endif]NOT語句。WhereNOT ;
[if !supportLists]五获三、[endif]高高級數(shù)據(jù)過濾----通配符進(jìn)行過濾
LIKE作為謂語出現(xiàn)旁蔼,來進(jìn)行模糊的匹配。(RegExp正則也可以來過濾疙教,之后補(bǔ))
[if !supportLists]1.????[endif]%
用“%”來代替任意字符串棺聊。Where pro_name like(‘F%y%’);
PS:??? 區(qū)分大小寫;
注意填充在字符串后面的空格(最好用函數(shù)去除);
% 無法區(qū)分NULL
[if !supportLists]2.????[endif]_
用 “_” 來代替單一字符,精確贞谓。
[if !supportLists]3.????[endif][]
用“[…]” 來指定一個字符集,[^…]來指定不包含的字符集
Seclect pro_name
From pro_tables
Where pro_name like ‘[JM]%’
Order by pro_name;
?????? 附注:RegExp來進(jìn)行正則表達(dá)式匹配限佩,使用方法一樣。
???????????????????? […]? [^…]?{n}? {n,m}?p1|p2|p3?
[if !supportLists]六、[endif]計算字段
應(yīng)用程序所需要的數(shù)據(jù)格式?jīng)]法直接seclect數(shù)據(jù)庫的列來呈現(xiàn)祟同,這時就需要創(chuàng)建計算字段作喘,直接在數(shù)據(jù)庫中創(chuàng)建、轉(zhuǎn)換晕城、計算泞坦。
[if !supportLists]1.????[endif]拼接字段
不同DBMS有不同的操作符。Access, sql server用“+” 砖顷,DB2贰锁,Oracle等用“|” 。
Select rtrim(vend_name) + ‘ (’ + rtrim(vend_cou) + ‘)’
As vend_title
From vendor
Order by vend_name;
附注:???? trim()函數(shù)去除字段的空格滤蝠,rtrim() 和ltrim()
?????????????? As()賦予別名
[if !supportLists]2.????[endif]執(zhí)行計算
Select price, quantity, price*quantity as expanded_price
From tables
Order by price;
[if !supportLists]七豌熄、[endif]數(shù)據(jù)處理函數(shù)
具體的DBMS中函數(shù)不同。在這里就不一一列舉了几睛,其實用法都很簡單房轿。
[if !supportLists]1.????[endif]文本處理函數(shù)
Left() ; rtrim() ; length() ; .soundex() ;
[if !supportLists]2.????[endif]日期、時間處理函數(shù)
看具體函數(shù)的參數(shù)意義所森。
[if !supportLists]3.????[endif]數(shù)值處理函數(shù)
這類函數(shù)在不同DBMS中用法相同囱持。
[if !supportLists]八、[endif]匯總數(shù)據(jù)
不需要實際的數(shù)據(jù)焕济,用聚集函數(shù)獲得匯總的數(shù)據(jù)纷妆,在所有的DBMS中用法一致。
[if !supportLists]1.????[endif]AVG ()
[if !supportLists]2.????[endif]COUNT ()
[if !supportLists]3.????[endif]MAX()
[if !supportLists]4.????[endif]MIN()
[if !supportLists]5.????[endif]SUM()
聚集不同值時使用distinct()晴弃。
[if !supportLists]九掩幢、[endif]分組數(shù)據(jù)
將數(shù)據(jù)分為多個邏輯組,對每個分組進(jìn)行聚集計算上鞠。
[if !supportLists]1.????[endif]用group by創(chuàng)建分組
Select vend_id , count(*) asvend_item
From products
Group by vend_id;
[if !supportLists]2.????[endif]用having 來過濾分組
Selectprod_id , sum(quantity) as sum_quan
Fromorderitems
whereitem_price >= 3
Group byprod_id
Havingsum(quantity) >=100
Order by prod_id;
[if !supportLists]3.????[endif]Select字句的順序
Select àfrom àwhere àgroup byàhaving àorder by
[if !supportLists]十际邻、[endif]使用子查詢
子查詢就是select的嵌套
在用子查詢過濾時注意表之間的聯(lián)結(jié)。
(Select count(*)
From cust
Where cust_id = prod_id)?as orders
[if !supportLists]十一芍阎、????[endif]聯(lián)結(jié)表
幾個表之間的聯(lián)結(jié)世曾。
[if !supportLists]1.????[endif]用where 實現(xiàn)的等值聯(lián)結(jié)
Where pro.prod_id = cust.cust_id
[if !supportLists]2.????[endif]內(nèi)聯(lián)結(jié)(inner join)
Select name , id , pro
From vender inner join cust
On vender.id = cust.id
????????????? 聯(lián)結(jié)多個表的時候,用where…and…
[if !supportLists]十二谴咸、????[endif]創(chuàng)建高級聯(lián)結(jié)
[if !supportLists]1.????[endif]自聯(lián)結(jié)
[if !supportLists]2.????[endif]Outer join
[if !supportLists]十三轮听、????[endif]組合查詢
Ouiou進(jìn)行組合查詢,與where 用法類似岭佳。Onion all
[if !supportLists]十四血巍、????[endif]插入數(shù)據(jù)
[if !supportLists]1.????[endif]用insert into插入數(shù)據(jù)
Insert into __ ()
Value ( )?;
insert into __ ()
select () from __;
[if !supportLists]2.????[endif]Select into復(fù)制數(shù)據(jù)表
Select *
From ()
Into __
在mysql中用法不同
Create table __ as select * from __;
[if !supportLists]十五、????[endif]更新和刪除數(shù)據(jù)
UPDATE? DELETE
[if !supportLists]1.????[endif]UPDATE __
SET __.__ =
FROM __;
[if !supportLists]2.????[endif]DELETE FROM __
WHERE __ =?? ;
[if !supportLists]十六珊随、????[endif]創(chuàng)建和操縱表
1.創(chuàng)建表Creat table __ (
***,
***
)
表列的名字和定義用“ , ”隔開
設(shè)置默認(rèn)值述寡,default __ 通常用于時間戳柿隙,MySQL中default current_date()
[if !supportLists]3.????[endif]更新表?alter table __
[if !supportLists]4.????[endif]刪除表drop table __
[if !supportLists]十七、????[endif]使用視圖
視圖即是打包的函數(shù)鲫凶,是虛擬的表
creat view __ as …
簡化主程序优俘。
[if !supportLists]十八、????[endif]使用存儲過程
即創(chuàng)建函數(shù)掀序,用時直接調(diào)用。
[if !supportLists]1.????[endif]執(zhí)行存儲過程
Execute __ (…);
[if !supportLists]2.????[endif]創(chuàng)建存儲過程
各個DBMS創(chuàng)建不同惭婿,步驟都差不多不恭。
[if !supportLists]十九、????[endif]管理事務(wù)處理
批量的進(jìn)行SQL語言操作财饥,保證遇到錯誤能夠回退换吧。
Transaction???rollback?? commit?? savepoint
[if !supportLists]二十、????[endif]游標(biāo)
Cursor