最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL中的基本查詢語(yǔ)句學(xué)習(xí)筆記

 更新時(shí)間:2016年03月24日 16:35:25   作者:GALAXY_ZMY  
這篇文章主要介紹了MySQL中的基本查詢語(yǔ)句學(xué)習(xí)筆記,包括使用limit限制查詢結(jié)果條數(shù)和合并查詢結(jié)果的方法,需要的朋友可以參考下

1.基本查詢語(yǔ)句
select 屬性列表 from 表名和視圖列表 [where 條件表達(dá)式1] [group by 屬性名1 [having 條件表達(dá)式2]] [order by 屬性名2 [asc|desc]]
2.單表查詢
1)使用*查詢所有字段

select * from 表名;

2) 查詢指定字段

select id,name from product;

使用上面例子可以查詢指定字段

3)查詢指定記錄
where 條件表達(dá)式
實(shí)例:

select *from employee where id = 1002;

where 子句常用查詢條件

比較:=、<、 <=、 >、 >=、 !=、 <>、 !>、 !<
指定范圍 : between and、not between and
指定集合:in、not in
匹配字符: like、not like
是否為空值:is null 、is not null
多條件查詢:and or
4)帶in關(guān)鍵字的查詢
in關(guān)鍵字可以判斷某個(gè)字段的值是否在指定的集合中。

[not] in (元素1,元素2,...,元素n)
實(shí)例:

select * from employee where id in (1001,1002);

如果集合中的元素為字符時(shí),需加上單引號(hào)。

5)帶between and 的范圍查詢
[not] between 取值1 and 取值2
取值1為起始值,取值2為終止值
實(shí)例:

select * from employee where age 
bewteen 15 and 20;

6)帶like的字符串匹配查詢
[not] like ‘字符串';
‘字符串'的值可以是完整的字符串,也可以是含百分號(hào)(%)或下滑線(_)的通配字符。

“% ”可以代表任意長(zhǎng)度的字符串,長(zhǎng)度可以是0。
“_”只能表示單個(gè)字符。
完整字符時(shí)like相當(dāng)于“=”。
實(shí)例:

select * from employee where homeaddr like ‘北京%';

查詢所有homeaddr字段中以“北京”
開(kāi)頭的記錄。

select * from employee where name like "ar_c";

查詢所有name字段值長(zhǎng)度為4,前兩個(gè)字母為“ar”最后一個(gè)字母為“c”的記錄。
統(tǒng)配的字符串可以用單引號(hào)或雙引號(hào)。
通配符“”可以多次使用,如“趙 _”。

7)查詢空置
is [not] null
實(shí)例:

select * from work where info is null;

查詢work表info字段為空的記錄。

8)and 和 or多條件查詢
條件表達(dá)式1 and 條件表達(dá)式2 [...and 條件表達(dá)式n]
and 表示同時(shí)滿足所有條件的記錄會(huì)被查詢出來(lái),or表示只要滿足其中一條的記錄就會(huì)被查詢出來(lái)。

9)查詢結(jié)果不重復(fù)
select distinct 屬性名
實(shí)例:

select distinct age department_id employee;

10) 查詢結(jié)果排序
order by 屬性名 [asc|desc]
默認(rèn)asc排序。
如果遇到某個(gè)字段存在空值的記錄,需要注意,空值排序時(shí)可以理解為該字段的最小值。
mysql中可以指定按多字段排序。
實(shí)例:

select * from employee order by id asc , age desc;


3.limit限制查詢結(jié)果條數(shù)
1)不指定起始位置
limit 記錄數(shù)
記錄數(shù)超過(guò)查詢結(jié)果則顯示所有的記錄,不會(huì)報(bào)錯(cuò)

2)指定起始位置
limit 起始位置 , 記錄數(shù)
記錄的起始位置從位置0開(kāi)始。

2.使用集合函數(shù)查詢
集合函數(shù)包括count(),sum(),avg(),max()和min()。
1)count()函數(shù)
統(tǒng)計(jì)記錄條數(shù)
實(shí)例:

select count(*) from employee;

與group by一起使用

select d_id,count(*) from employee group by d_id;

上述語(yǔ)句會(huì)先分組后統(tǒng)計(jì)。

2) sum()函數(shù)
sum()函數(shù)是求和函數(shù)
實(shí)例:

select num,sum(score) from grade where num= 1001;

select num,sum(score) from grade group by num;

sum()只能計(jì)算數(shù)值類(lèi)型字段。
3)avg()函數(shù)
avg()函數(shù)是求平均值函數(shù)。
實(shí)例:

select avg(age) from employee;

select course,avg(score) from group by course;

4)max(),min()函數(shù)
求最大值和最小值。
實(shí)例:

select max(age) from employee;
select num,course,max(score) from grade group by course;

對(duì)于字符串的最大值問(wèn)題,max()函數(shù)是使用字符對(duì)應(yīng)的ascii碼進(jìn)行計(jì)算的。

4.合并查詢結(jié)果
使用union和union all關(guān)鍵字。
union將查詢的結(jié)果合并到一起并去掉形同的記錄,union all 只是簡(jiǎn)單地合并到一起。

select 語(yǔ)句1 union|union all
select 語(yǔ)句2 union|union all...
select 語(yǔ)句n;
PS:為表或字段起別名
表起別名語(yǔ)法:

表名 表的別名

select * from department d where d.d_id =1001;

字段起別名語(yǔ)法:

屬性名 [as] 別名
as可有可無(wú)。

select d_id as department_id,d_name as department_name from department;

相關(guān)文章

最新評(píng)論

乐亭县| 邹城市| 子洲县| 塔河县| 新源县| 盐山县| 桓仁| 福安市| 潜江市| 治县。| 清涧县| 德保县| 金阳县| 蕲春县| 张家界市| 花垣县| 藁城市| 大新县| 肥西县| 五峰| 越西县| 陵川县| 芦山县| 辛集市| 电白县| 常德市| 石屏县| 昌平区| 墨江| 固阳县| 宝应县| 桦甸市| 周口市| 宁津县| 乐业县| 神农架林区| 元谋县| 云和县| 达州市| 陵川县| 米易县|