代碼小白產(chǎn)品經(jīng)理,要懂的mysql語(yǔ)言(一)
9.子句查詢(xún)的陷阱
在代碼編程中,以下五種子句有嚴(yán)格順序,以以下排序?yàn)闇?zhǔn):
where、group by、having、order by、limit
1)where型子查詢(xún)
內(nèi)層查詢(xún)的結(jié)果作為外層比較的條件。
從某表的某列,查詢(xún)出該列范圍內(nèi)的某些信息
? ? -> select cat_id,goods_name,goods_price from goods where goods_id=(select max(goods_id) from goods)
? ? -> select cat_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id)
2)from型子查詢(xún)
從某表的某列種查詢(xún)某些信息,此信息作為下一層查詢(xún)根,查詢(xún)某些信息
? ? -> select cat_id,goods_id,goods_name from asc_goods where goods_id=(select max(goods_id) from asc_goods)
3)exists型子查詢(xún)
查詢(xún)哪個(gè)列下存在某些信息。
以“A表”為基礎(chǔ),查詢(xún)出“A表”和“B表”在某一列信息中同時(shí)存在的信息。
? ? -> select * from category where exists(select * from goods where goods.cat_id=category.cat_id)
10.內(nèi)連接查詢(xún)
? ? -> select boy_id, boy_name,girl_id,girl_name from boy inner join girl on boy_id=girl_id
11.左右連接查詢(xún)
以左邊/右邊數(shù)據(jù)為準(zhǔn),查到則連接,查不到則顯示null
? ? -> select boy_id, boy_name,girl_id,girl_name from boy left join girl on boy_id=girl_id
12.union查詢(xún)
把兩條或多條sql查詢(xún),合并成一個(gè)查詢(xún)結(jié)果集;
union語(yǔ)句必須滿(mǎn)足一個(gè)條件:各語(yǔ)句取出的列數(shù)相同;
列名稱(chēng)未必要一致,列名稱(chēng)會(huì)以使用的第一條sql列名稱(chēng)為準(zhǔn);
使用union時(shí),完全相等的行,將會(huì)被合并,合并是一個(gè)耗時(shí)操作,一般不讓union進(jìn)行,合并使用“union all”可以避免合并;
union子句中不用寫(xiě)order by,因?yàn)閟ql合并后得到的總結(jié)果,可以order by ,子句order by失去意義。
? ? -> select user_id,name from user
? ? -> union
? ? -> select user_id,name from tmp
? ? -> select user_id,name from user
? ? -> union all
? ? -> select user_id,name from tmp
? ? -> select id,sum(goods_price) from?
? ? -> select id,goods_price from A
? ? -> union
? ? ->?select id,sum(goods_price) from
? ? -> (select id,goods_price from A
? ? -> union all
? ? -> select id,goods_price from B)
13.創(chuàng)建表table過(guò)程
理解表與列的關(guān)系,掌握基本建表語(yǔ)法。
建表語(yǔ)句:
create table A<
列1【列類(lèi)型、列屬性、默認(rèn)值】
...
列n【列類(lèi)型、列屬性、默認(rèn)值】
>
?create table A <
? ? -> sn int
? ? -> name varcher<10>
? ? -> >
? ? -> show tables
14.整型列
數(shù)值型:整型、浮點(diǎn)型、定點(diǎn)型
字符串:char、varchar、text
日期時(shí)間類(lèi)型:XXXX-XX-XX hh:mm:ss
15.整行列可選參數(shù)的問(wèn)題
1)tinyint帶符號(hào),數(shù)值范圍-128~127
創(chuàng)建B表,表中的字段為num,數(shù)值范圍限制在-128~127,添加該字段下的數(shù)據(jù),若數(shù)據(jù)不在此范圍內(nèi),無(wú)法添加。
? ? -> create table B<
? ? -> num tinyint
? ? -> >
? ? -> insert into B values<127>
2)unsigned無(wú)符號(hào),列的值從0開(kāi)始,無(wú)負(fù)值
邏輯同tinyint,不同點(diǎn)是,unsigned的數(shù)值范圍>=0
? ? -> alter table B add unum tinyint unsigned
? ? -> insert into B values <3,255>
? ? -> alter table B add
3)zerofill適用于學(xué)號(hào)、編碼等固定寬度數(shù)字,用0填充至固定寬度
zerofill屬性默認(rèn)決定列為unsigned
? ? -> alter table B add sn tinyint<5> zerofill
? ? -> insert into B values<4,4,123>
16.浮點(diǎn)列與定點(diǎn)列
float小數(shù)點(diǎn)數(shù),float[M,D].[unsigned].[zerofill],M代表小數(shù)點(diǎn)總位數(shù),D代表小數(shù)點(diǎn)后邊的位數(shù);
double和float相同,不同點(diǎn)在于double范圍比f(wàn)loat大很多。
? ? -> create table B <
? ? -> salary float <4,3>
? ? -> salary2 float <3,2>
? ? -> >
? ? -> insert into B values <2.012,2.13>
? ? -> select * from B
17.字符型列
1)char定長(zhǎng):char<M>,char<10>;
varchar變長(zhǎng):varchar<M>,varhcar<10>
若定義一個(gè)char[10]和varchar[10],如果存進(jìn)去的是‘a(chǎn)bcd’,那么char所占的長(zhǎng)度依然為10,除了字符‘a(chǎn)bcd’外,后面跟六個(gè)空格,而varchar就立馬把長(zhǎng)度變?yōu)?了,取數(shù)據(jù)的時(shí)候,char類(lèi)型的要用trim()去掉多余的空格,而varchar是不需要的。
? ? -> create table B <
? ? -> name char <10>
? ? -> name2 varchar <10>
? ? -> >
2)text:可存一大段文字
? ? -> create table B <
? ? -> tx text
? ? -> >
3)blob:可存圖像,不會(huì)因字符集問(wèn)題,信息丟失
? ? -> create table B <
? ? -> tx2 blob
? ? -> >
4)enum:枚舉型,定好值就在枚舉范圍內(nèi)
? ? -> create table B <
? ? ->?sex2 set <'不知道','不清楚'>
? ? -> >
? ? -> insert into B <set> values <'不知道'>
18.日期和時(shí)間列
年、日期、時(shí)間、日期時(shí)間、時(shí)間戳(從**到當(dāng)前時(shí)間)
timestamp時(shí)間戳:自動(dòng)默認(rèn)或更新
? ? -> create table B <
? ? -> ya year
? ? -> dt date
? ? -> tm time
? ? -> dttm datetime
? ? -> >
? ? -> inset into B <ya,dt,tm,dttm> values <1980,1980-10-20,12:30:45,1980-12-23 12:23:25>
? ? -> select * from B
? ? -> create table B <
? ? -> id int
? ? -> tt timestamp
? ? -> >
? ? -> insert into B <id> values <1>
? ? -> select * from B