您好,登錄后才能下訂單哦!
今天小編給大家分享一下MySQL中有哪些數據查詢語句的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
1、“select * from 表名;”,—可查詢表中全部數據;
2、“select 字段名 from 表名;”,—可查詢表中指定字段的數據;
3、“select distinct 字段名 from 表名;”,—可對表中數據進行去重查詢。
4、“select 字段名 from 表名 where 查詢條件;”,—可根據條件查詢表中指定字段的數據;
1)比較運算符:>, <, >=, <=, =, !=, <>
查詢大于18歲的信息
select * from students where age>18; select id, name,gender from students where age>18;查詢小于18歲的信息
select * from students where age<18;查詢年齡為18歲的所有學生的名字
select * from students where age=18;
2)邏輯運算符:and, or, not
–18到28之間的學生信息
select * from students where age>18_and age<28:–18歲以上的女性
select * from students where age>18 and gender="女"; select * from students where age>18 and gender=2;–18以上或者身高查過180(包含)以上
select * from students where age>18 or height>=180;不在18歲以上的女性這個范圍內的信息
select * from students where not (age>18 and gender=2);年齡不是小于或者等于18并且是女性
select * from students where (not age<=18) and gender=2;
3)模糊查詢:like, rlike
% 替換1個或者多個
_ 替換1個
查詢姓名中 以“小”開始的名字select name from students where name="小"; select name from students where name like"小%";查詢姓名中有“小”所有的名字
select name from students whece name like "%小%";查詢有2個字的名字
select name from students where name like "__";查詢有3個字的名字
select name from students where name like "__";查詢至少有2個字的名字 select name from
students where name like "__%";rlike正則
查詢以周開始的姓名select name from students where name rlike "^周.*";查詢以周開始、倫結尾的姓名
select name from students where name rlike "^周.*倫$";
4)范圍查詢:in,not in,between…and,not between…and
查詢年齡為18、34的姓名
select name, age from students where age=18 or age=34; select name,age from students where age in (18,34);not in不非連續的范圍之內
年齡不是 18、34歲之間的信息select name,age from students where age not in (18,34);between … and …表示在一個連續的范圍內
查詢年齡在18到34之間的的信息select name,age from students where age between 18 and 34;not between … and …表示不在一個連續的范圍內
查詢年齡不在在18到34之間的的信息select * from students where age not between 18 and 34;
空判斷
判空 is null
查詢身高為空的信息select *from students where height is null/NULL/Null;判非空is not null
select * from students where height is not null;
排序:order_by
–查詢年齡在18到34歲之間的男性,按照年齡從小到大排序
select * from students where (age between 18 and 34) and gender=1; select * from students where (age between 18 and 34) and gender=1 order by age; select * from students where (age between 18 and 34) and gender=1 order by age asc;查詢年齡在18到34歲之間的女性,身高從高到矮排序
select * from students where (age between18 and 34) and gender=2 order by height desc;order by多個字段
查詢年齡在18到34歲之間的女性,身高從高到矮排序,如果身高相同的情況下按照年齡從小到大排序select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc;查詢年齡在18到34歲之間的女性,身高從高到矮排序,如果身高相同的情況下按照年齡從小到大排序,如果年齡也相同那么按照id從大到小排序
select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc, id desc;按照年齡從小到大、身高從高到矮的排序
select * from students order by age asc,height desc;
分組:group_by, group_concat():查詢內容, having
where :是對整個數據表信息的判斷;
having:是對于分組后的數據進行判斷
–group by
按照性別分組,查詢所有的性別select gender from students group by gender;–計算每種性別中的人數
select gender, count(*) from students group by gender;where是在group by 前面
–計算男性的人數select count(*) from students where gender='男';–group_concat(…)
查詢同種性別中的姓名select gender,group_concat(name) from students group by gender;having :having是在group by后面
查詢平均年齡超過30歲的性別,以及姓名select gender ,avg(age) from students group by gender having avg(age) > 30;查詢每種性別中的人數多于2個的信息
select gender,count(*) from students group by gender having count(*) > 2;– 查詢每組性別的平均年齡
select gender,avg(age) from students group by gender;
分頁: limit
limit start,count (start:表示從哪─個開始;count:表示數量) 即limit(第N頁-1)*每個的個數,每頁的個數; limit在使用的時候,要放在最后面.
限制查詢出來的數據個數
select *from students where gender=1 limit 2;查詢前5個數據
select* from students limit 0,5;查詢id6-10(包含)的書序
select * from students limit 5,5;每頁顯示2個,第1個頁面
select * from students limit 0,2;每頁顯示2個,第2個頁面
select * from students limit 2,2;每頁顯示2個,第3個頁面
select * from students limit 4,2;每頁顯示2個,第4個頁面
select * from students limit 6,2;每頁顯示2個,顯示第6頁的信息,按照年齡從小到大排序
select * from students order by age asc limit 10,2;– 如果重新排序了,那么會顯示第一頁
select * from students where gender=2 order by height des limit 0,2;
5)聚合函數:count(), max(), min(), sum(), avg(), round()
聚合函數
-總數-- count
-查詢男性有多少人,女性有多少人select count(*) from students where gender=1; select count(*) as 男性人數 from students where gender=1; select count(*) as 女性人數 from students where gender=2;-最大值-最小值
– max --min
一查詢最大的年齡select max (age) from students;–查詢女性的最高身高
select max (height) from students where gender=2;-求和
–sum
-計算所有人的年齡總和select sum ( age) from students;–平均值
– avg
–計算平均年齡select avg(age) from students;–計算平均年齡
select sum ( age) / count(* ) from students;–四舍五入round ( 123.23 ,_1)保留1位小數
–計算所有人的平均年齡,保留2位小數select round (sum(age)/count(*),2) from students; select round ( sum(age)/count(*),3) from students ;–計算男性的平均身高保留2位小數
select round(avg (height),2) from students where gender=1; select name,round(avg(height),2) from students where gender=1;
6)連接查詢 :inner join, left join, right join
inner join
select … from 表 A inner join表B;select * from students inner join classes;查詢有能夠對應班級的學生以及班級信息
select * from students inner join classes on students.cls_id=classes.id;按照要求顯示姓名、班級
select students.*, classes.name from students inner join classes on students.cls_id=classes.id; select students.name,classes.name from students inner join classes on students.cls_id=classes.id;給數據表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;查詢有能夠對應班級的學生以及班級信息,顯示學生的所有信息,只顯示班級名稱
select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;在以上的查詢中,將班級姓名顯示在第1列
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;查詢有能夠對應班級的學生以及班級信息,按照班級進行排序
select c.xxx s.xxx from student as s inner join clssses as c on … order by …;select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;當時同一個班級的時候,按照學生的id進行從小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;left join
查詢每位學生對應的班級信息select * from students as s left join classes as c on s.cls_id=c.id;查詢沒有對應班級信息的學生
– select … from xxx as s left join xxx as c on… where …
– select … from xxx as s left join xxx as c on… . … having …select * from students as s left join classes as c on s.cls_id=c.id having c.id is null; select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;
left join是按照左邊的表為基準和右邊的表進行查詢,查到就顯示,查不到就顯示為null
補充
查詢所有字段:select * from 表名;
查詢指定字段:select 列1,列2,... from 表名;
使用 as 給字段起別名: select 字段 as 名字.... from 表名;
查詢某個表的某個字段:select 表名.字段 .... from表名;
可以通過 as 給表起別名: select 別名.字段 .... from 表名 as 別名;
消除重復行: distinct 字段
注意:WHERE子句中是不能用聚集函數作為條件表達式的!
二、總結
(1)命令:select * from <表名>;
(2)命令:select <要查詢的字段> from <表名>;
命令:select distinct <要查詢的字段> from <表名>
升序:asc
降序:desc
降序排列命令:select <要查詢的字段名> from <表名> order by <要查詢的字段名> desc
不加desc一般默認為升序排列
命令:select <按什么分的組>, Sum(score) from <表名> group by <按什么分的組>
假設現在又有一個學生成績表(result)。要求查詢一個學生的總成績。我們根據學號將他們分為了不同的組。
命令:
select id, Sum(score) from result group by id;
現在有兩個表學生表(stu)和成績表(result)。
當連接運算符為“=”時,為等值連接查詢。
現在要查詢年齡小于20歲學生的不及格成績。
select stu.id,score from stu,result where stu.id = result.id and age < 20 and score < 60;
等值查詢效率太低
①語法
select f1,f2,f3,.... from table1 left/right outer join table2 on 條件;
②左外連接查詢,例如
select a.id,score from (select id,age from stu where age < 20) a (過濾左表信息) left join (select id, score from result where score < 60) b (過濾右表信息) on a.id = b.id;
左外連接就是左表過濾的結果必須全部存在。如果存在左表中過濾出來的數據,右表沒有匹配上,這樣的話右表就會出現NULL;
③右外連接查詢,例如
select a.id,score from (select id,age from stu where age < 20) a (過濾左表信息) right join (select id, score from result where score < 60) b (過濾右表信息) on a.id = b.id;
右外連接就是左表過濾的結果必須全部存在
①語法
select f1,f2,f3,.... from table1 inter join table2 on 條件;
②例如
select a.id,score from (select id,age from stu where age < 20) a (過濾左表信息) inner join (select id, score from result where score < 60) b (過濾右表信息) on a.id = b.id;
在圖書表(t_book)和圖書類別表(t_bookType)中
①.union
使用union關鍵字是,數據庫系統會將所有的查詢結果合并到一起,然后去掉相同的記錄;
select id from t_book union select id from t_bookType;
②.union all
使用union all,不會去除掉重復的記錄;
select id from t_book union all select id from t_bookType;
以上就是“MySQL中有哪些數據查詢語句”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。