--查詢基本使用(條件,排序,聚合函數,分組,分頁) --建立學生表 create table students ( id int unsigned not null auto_increment primary key, name varchar(20) default '', age tinyint unsigned default 0, high decimal(5,2), gender enum('男', '女', '中性', '保密') default '保密', cls_id int unsigned default 0, is_delete bit default 0 ); --建立班級表 create table classes( id int unsigned auto_increment primary key not null, name varchar(20) not null ); --往students表裏插入數據 insert into students values (0,'小明',18,180.00,1,1,0), (0,'小月月',19,180.00,1,2,0), (0,'彭于晏',28,185.00,1,1,0), (0,'劉德華',58,175.00,1,2,0), (0,'黃蓉',108,160.00,2,1,0), (0,'鳳姐',44,150.00,4,2,1), (0,'王祖賢',52,170.00,2,1,1), (0,'周杰倫兒',34,null,1,1,0), (0,'程坤',44,181.00,1,2,0), (0,'和珅',55,166.00,1,2,0), (0,'劉亦菲',29,162.00,2,3,0), (0,'金星',45,180.00,3,4,0), (0,'靜香',18,170.00,2,4,0), (0,'郭靖',22,167.00,1,5,0), (0,'周杰',33,178.00,1,1,0), (0,'錢小豪',56,178.00,1,1,0), (0,'謝霆鋒',38,175.00,1,1,0), (0,'陳冠希',38,175.00,1,1,0); --查詢 -- 查詢全部列 --select * from 表名 select * from students; --必定條件查詢(where) select * from where id=5; -- 查詢制定列 select id,name from students; -- 使用as給字段起別名 select id,name as '姓名', age, high, gender from students; -- 經過表名字段查詢 select students.name from students; -- 給表起別名查詢 select s.id,s.name,s.age from students as s; --消除重複行 -- distinct select distinct age from students; --條件查詢 --比較運算符 -- 查詢年紀大於18歲的信息 select * from students where age > 18; --18歲到28歲之間(and) select * from students where age >= 18 and age =< 28; select * from students where age between 18 and 28 --在18歲以上或者身高180以上的人(or) select * from students where age > 18 or high > 180; -- 模糊查詢 -- like -- % 替代1個或者多個甚至是沒有 -- 查詢姓名中有‘小’的全部名字 select * from students where name like '%小%'; -- 查詢兩個字人的名字 select * from students where name like '__'; -- 查詢至少有2個字的名字 select * from students where name like '%__%'; --範圍查詢 -- in (1,3,8)表示在一個非連續的範圍內 -- 查詢 年紀爲18和34的人 select * from students where age in (18, 34); --查詢 年齡在17歲到34歲之間的信息 select * from students where age between 17 and 34; --查詢 年紀不在18到34歲的信息 select * from students where age not between 17 and 34; -- 空判斷 -- 判斷is null -- 查詢身高爲空的信息 select * from students where high is null; -- 判斷非空is not null select * from students where high is not null; -- 排序 -- order by 字段 -- asc從小到大排列,即升序 -- desc從大到小排序,即降序 -- 查詢年紀在18到34歲之間的男性,按照年紀從小到大 select * from students where gender=1 and age between 18 and 34 order by age; -- 查詢年紀在18到34歲之間的女性,身高從高到矮 select * from students where gender=2 and age between 18 and 34 order by high desc; -- order by 多字段 -- 查詢年紀在18到34歲的女性,身高從高到矮排序,若是身高相同的狀況下按照年紀從小到大排序 select * from students where age between 18 and 34 and gender=2 order by high desc; -- 查詢年紀在18到34歲的男性,身高從高到矮排序,若是身高相同的狀況下按照年紀從小到大排序,若是年齡也相等那麼按照id從小到大排序; select * from students where age between 18 and 34 and gender=1 order by high desc, age, id desc; --聚合函數 -- 總數 -- count -- 查詢男性有多少人 select count(*) from students where gender=1; -- 最大值 -- max -- 查詢最大的年紀 select max(age) from students; -- 查詢女性的最高 身高 select max(high) from students where gender=2; -- 最小值 -- min select min(high) from students; -- 求和 -- sum -- 計算全部人的年齡總和 select sum(age) from students; -- 平均值 -- avg -- 計算平均年紀 -- 計算平均年紀 sum(age)/count(*) select sum(age)/count(*) from students; select avg(age),2 from students; -- 保留2位小數 select round(avg(age),2) from students; -- 分組 -- group by -- 按照性別分組,查詢全部的性別 select gender from students group by gender; -- 計算每組性別的人數 select gender, count(*) from students group by gender; -- 查詢男性組中的姓名 group_concat select gender,group_concat(name) from students where gender=1 group by gender; -- having -- 查詢每一個性別平均年紀超過30歲的性別,以及姓名 having avg(age) > 30 select gender, group_concat(name) from students group by gender having avg(age) > 30; -- 查詢每種性別中的人數多於4個的組的信息 select gender,group_concat(name) from students group by gender having count(*)>4; -- 分頁 -- 顯示5頁 select * from students limit 5; -- 分頁顯示,每頁顯示2條數據 select * from students limit 0, 2; -- 按照身高從高到矮排序,查找出全部女性,而且分頁顯示,每頁顯示2條數據 select * from students where gender=2 order by high desc limit 0,2;