学习笔记(十五)——MySQL(查询)
生活随笔
收集整理的這篇文章主要介紹了
学习笔记(十五)——MySQL(查询)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
查詢方法
下面的查詢都是對單表的查詢,所以先創建表tb1(下面的tb1均代表此表)
create table tb1(id int,name varchar(10),age int,class varchar(5)); #創建tb1表對于MySQL的創建、插入等基本語句有不理解的可以看我上一篇博客:學習筆記(十四)——MySQL(CRUD)
1、根據條件查詢
select * from tb1 where age=19; #查詢年齡所有等于19歲 select * from tb1 where age>19; #查詢年齡所有大于19歲 select * from tb1 where age>=19; #查詢年齡所有大于等于19歲 select * from tb1 where class!='two'; #查詢班級不為two的1. is null和is not null
select * from tb1 where class is null; #查詢班級為空 select * from tb1 where class is not null; #查詢班級非空2.and、or 、not
select * from tb1 where age=19 and class='two'; #and 和 所有條件都要滿足 select * from tb1 where age=19 or class='two'; #or 或者 滿足其中一個條件 select * from tb1 where not age=19; # not 非 即條件的否定 查詢年齡不等于19 select * from tb1 where age!=19; #查詢年齡不等于192、排序
關鍵字:asc、desc
select * from tb1 order by age; #默認升序 select * from tb1 order by age asc; #升序 select * from tb1 order by age desc; #降序3、限制
limit m,n 從下標為m的行數開始顯示n條數據
select *from tb1 limit 3; #只顯示3行,即前三行 select *from tb1 limit 3,3; #顯示的是從第4行開始,顯示3行 select *from tb1 order by age desc limit 3; #取年齡最高的前三名4、去重
注意關鍵字distinct
select distinct * from tb1; #去掉有重復的數據5、模糊查詢
select * from tb1 where name like '%'; % 代表任意多個字符 select * from tb1 where name like 'zhang%'; 查詢name以zhang開頭的 select * from tb1 where name like 'zhang___'; _ 代表一個任意字符 3個_代表三個字符6、范圍查詢
關鍵字:between···and
select * from tb1 where age between 19 and 22; #查詢年齡在19-20歲 select * from tb1 where 19<=age<=22; #查詢年齡在19-20歲 select * from tb1 where class in ('one','three'); #查詢班級為one和three的7、聚合函數
select max(age) from tb1; #求最大值,這里值年齡最大值 select min(name) from tb1; #求最小值 ,字符串按照26個字母排序 select count(name) from tb1; #統計字段值不為空的個數 select count(*) from tb1; #計算數據總數 #注意:count 可以寫* select avg(age) from tb1; #求平均值 select sum(age) from tb1; #求和 select group_concat(name) from tb1; #列出字段所有的值8、分組查詢
分組查詢通常和聚合函數一起使用。
在分組情況下,只能出現分組字段和聚合字段,其他字段沒有意義,會報錯。
select class from tb1 group by class; #根據class分組 select class,count(name) from tb1 group by class; #計算每個年級的人數 select class,avg(age) from tb1 group by class; #計算每個年級的平均人數9、聚合篩選
select class,count(name) from tb1 group by class having count(name)=5; 篩選count(name)=5 select class,count(name) from tb1 group by class having class='two'; 篩選class='two'10、子查詢
select * from tb1 where age>(select avg(age) from tb1); #括號不能少 查找年齡大于平均年齡的內連接與外連接
內連接與外連接是針對多表操作的,所以這里先創建表tb2和tb3。(下面的tb2和tb3都是代表這兩個表)
create table tb2( #創建表tb2 id int, name varchar(20), gender enum('0','1'), join_date datetime, dept_id int ); create table tb3( #創建表tb3 dept_id int, dept_name varchar(20), salary int );1、內連接
指連接結果僅包含符合連接條件的行,參與連接的兩個表都應該符合連接條件。
基本語法:
左表 [inner] join 右表 on 左表.字段 = 右表.字段;1. 無條件內連接
select * from tb2 inner join tb3; #不推薦,假設tb2有10條數據,tb3有5條,無條件連接后就有50條2.有條件內連接
select * from tb2 inner join tb3 on tb2.dept_id=tb3.dept_id;2、外連接
連接結果不僅包含符合連接條件的行同時也包含自身不符合條件的行。
1.左外連接
左表的全部展示出來 右邊只會顯示符合搜索條件的。
2.右外連接
右邊表數據行全部保留,左邊表保留符合連接條件的行。
總結
以上是生活随笔為你收集整理的学习笔记(十五)——MySQL(查询)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记——Numpy基本操作(二)
- 下一篇: 学习笔记(十六)——MySQL(约束与关