MySQL中的基本查詢語句學習筆記_Mysql

來源:互聯網
上載者:User

1.基本查詢語句
select 屬性列表 from 表名和視圖列表 [where 條件運算式1] [group by 屬性名稱1 [having 條件運算式2]] [order by 屬性名稱2 [asc|desc]]
2.單表查詢
1)使用*查詢所有欄位

select * from 表名;

2) 查詢指定欄位

select id,name from product;

使用上面例子可以查詢指定欄位

3)查詢指定記錄
where 條件運算式
執行個體:

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關鍵字的查詢
in關鍵字可以判斷某個欄位的值是否在指定的集合中。

[not] in (元素1,元素2,...,元素n)
執行個體:

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

如果集合中的元素為字元時,需加上單引號。

5)帶between and 的範圍查詢
[not] between 取值1 and 取值2
取值1為起始值,取值2為終止值
執行個體:

select * from employee where age bewteen 15 and 20;

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

“% ”可以代表任意長度的字串,長度可以是0。
“_”只能表示單個字元。
完整字元時like相當於“=”。
執行個體:

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

查詢所有homeaddr欄位中以“北京”
開頭的記錄。

select * from employee where name like "ar_c";

查詢所有name欄位值長度為4,前兩個字母為“ar”最後一個字母為“c”的記錄。
統配的字串可以用單引號或雙引號。
萬用字元“”可以多次使用,如“趙 _”。

7)查詢空置
is [not] null
執行個體:

select * from work where info is null;

查詢work表info欄位為空白的記錄。

8)and 和 or多條件查詢
條件運算式1 and 條件運算式2 [...and 條件運算式n]
and 表示同時滿足所有條件的記錄會被查詢出來,or表示只要滿足其中一條的記錄就會被查詢出來。

9)查詢結果不重複
select distinct 屬性名稱
執行個體:

select distinct age department_id employee;

10) 查詢結果排序
order by 屬性名稱 [asc|desc]
預設asc排序。
如果遇到某個欄位存在空值的記錄,需要注意,空值排序時可以理解為該欄位的最小值。
mysql中可以指定按多欄位排序。
執行個體:

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


3.limit限制查詢結果條數
1)不指定起始位置
limit 記錄數
記錄數超過查詢結果則顯示所有的記錄,不會報錯

2)指定起始位置
limit 起始位置 , 記錄數
記錄的起始位置從位置0開始。

2.使用集合函數查詢
集合函數包括count(),sum(),avg(),max()和min()。
1)count()函數
統計記錄條數
執行個體:

select count(*) from employee;

與group by一起使用

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

上述語句會先分組後統計。

2) sum()函數
sum()函數是求和函數
執行個體:

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

sum()只能計算數實值型別欄位。
3)avg()函數
avg()函數是求平均值函數。
執行個體:

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

4)max(),min()函數
求最大值和最小值。
執行個體:

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

對於字串的最大值問題,max()函數是使用字元對應的ascii碼進行計算的。

4.合并查詢結果
使用union和union all關鍵字。
union將查詢的結果合并到一起並去掉形同的記錄,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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.