MySQL中的基本查詢語句學(xué)習(xí)筆記
1.基本查詢語句
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í),需加上單引號。
5)帶between and 的范圍查詢
[not] between 取值1 and 取值2
取值1為起始值,取值2為終止值
實(shí)例:
select * from employee where age bewteen 15 and 20;
6)帶like的字符串匹配查詢
[not] like ‘字符串';
‘字符串'的值可以是完整的字符串,也可以是含百分號(%)或下滑線(_)的通配字符。
“% ”可以代表任意長度的字符串,長度可以是0。
“_”只能表示單個(gè)字符。
完整字符時(shí)like相當(dāng)于“=”。
實(shí)例:
select * from employee where homeaddr like ‘北京%';
查詢所有homeaddr字段中以“北京”
開頭的記錄。
select * from employee where name like "ar_c";
查詢所有name字段值長度為4,前兩個(gè)字母為“ar”最后一個(gè)字母為“c”的記錄。
統(tǒng)配的字符串可以用單引號或雙引號。
通配符“”可以多次使用,如“趙 _”。
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ì)被查詢出來,or表示只要滿足其中一條的記錄就會(huì)被查詢出來。
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ù)超過查詢結(jié)果則顯示所有的記錄,不會(huì)報(bào)錯(cuò)
2)指定起始位置
limit 起始位置 , 記錄數(shù)
記錄的起始位置從位置0開始。
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;
上述語句會(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ù)值類型字段。
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;
對于字符串的最大值問題,max()函數(shù)是使用字符對應(yīng)的ascii碼進(jìn)行計(jì)算的。
4.合并查詢結(jié)果
使用union和union all關(guān)鍵字。
union將查詢的結(jié)果合并到一起并去掉形同的記錄,union all 只是簡單地合并到一起。
select 語句1 union|union all
select 語句2 union|union all...
select 語句n;
PS:為表或字段起別名
表起別名語法:
表名 表的別名
select * from department d where d.d_id =1001;
字段起別名語法:
屬性名 [as] 別名
as可有可無。
select d_id as department_id,d_name as department_name from department;
相關(guān)文章
MySQL如何對數(shù)據(jù)進(jìn)行排序圖文詳解
我們知道從MySQL表中使用SQL SELECT語句來讀取數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MySQL如何對數(shù)據(jù)進(jìn)行排序的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Mysql排序和分頁(order by&limit)及存在的坑
這篇文章主要介紹了Mysql排序和分頁(order by&limit)及存在的坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09phpmyadmin出現(xiàn)#2003服務(wù)器無響應(yīng)解決方法小結(jié)
出現(xiàn)登陸phpmyadmin出現(xiàn) #2003 - 服務(wù)器沒有響應(yīng)最先想到的是你的mysql服務(wù)器是不是停止了檢查一下,如果是mysql服務(wù)器停止服務(wù)了重啟就可以解決問題了2012-04-04mysql優(yōu)化連接數(shù)防止訪問量過高的方法
這篇文章主要介紹了mysql優(yōu)化連接數(shù)防止訪問量過高的方法,需要的朋友可以參考下2014-06-06