天天看點

工欲善其事必先利其器——MySQL資料庫(2),查詢操作

  • 一般查詢
  • 查詢所有字段
select * from 表名;
      
  • 查詢指定字段
select 列1,列2,... from 表名;
      
  • 使用 as 給字段起别名
select 字段 as 别名 ... from 表名;
      
  • 使用 as 給表起别名
select 别名.字段 .... from 表名 as 别名;
在目前的sql 語句中 臨時的給students 起了一個别名叫做s
錯誤: select students.name, students.gender, students.age from students as s;
select s.name, s.gender, s.age from students as s; # 臨時的給students 起了一個别名叫做s
      
  • sql語句完全的形式

隻有正在使用目前資料庫, 并且查找的資料表就在目前資料庫中,才可以省略資料庫名

資料表名什麼時候不可以省略: 當一個sql語句中出現了多個資料表, 并且查詢的字段在多個表中并且字段名相同, 此時表名不能夠省略

例如查詢 classes 表和 students 表中的name:
錯誤: select name,name from classes,students;
select students.name,classes.name from classes,students;
      
  • 消除重複行
distinct 字段, 修飾所有需要查詢的字段
select distinct 字段 from 表名;
      
  • 條件查詢
  • 比較運算符
">" : 查詢年齡大于 18 的資訊
 select * from 表名 where age > 18;
 "<"
 ">="
 "<="
 "=":注意 SQL 中等于就是 "=",不是 "=="
 "!=" 或 "<>"
      
  • 邏輯運算符
"and":18 歲以上的女性
 select * from 表名 where age > 18 and gender = "女";
 "or":18 歲以上或者為女性
 select * from 表名 where age > 18 or gender = "女";
 "not":年齡不是 18 歲的資訊
 select * from students where age != 18;
 select * from students where not age = 18;
      
  • 模糊查詢
"like"
 % 表示任意字元可有可無
 查詢姓名中有'黃'的名字
 select * from students where name like "%黃%";
 _ 表示任意一個字元
 查詢有2個字的名字
 select * from students where name like "__";
 "rlike":正則
 查詢以黃開始的姓名
 select * from students where name rlike "^黃.*";
      
  • 範圍查詢
"in":表示在一個非連續的範圍内
 查詢年齡為 18 和 34 的學生
 select * from students where age = 18 or age = 34;
 select * from students where age in (18,18,34);
 "not in":表示不再連續的範圍内
 查詢年齡不是 18 和 34 的學生
 select * from students where age not in (18,34);
 "between ... and ...":表示在一個連續的範圍内,兩邊都會包含,是閉區間.
 查詢年齡在 18 到 34 之間的資訊
 select * from students where age between 18 and 34;
 "not between ... and ...":表示不再一個連續的範圍内
 ```
 - 空判斷 null, 不能使用比較運算符
 ```
 select * from 表名 where 字段 is NULL;
 select * from 表名 where 字段 is not NULL;
 注意不能使用 not is
      
  • 排序
order by 字段 asc(升序)|desc(降序) 預設就是升序排序 asc 可以省略
      
  • 降序 desc
查詢年齡在18到34歲之間的女性,身高從高到矮排序
select * from students where age between 18 and 34 and gender = 2 order by height desc;
order by 多個字段 order by age asc, height desc
查詢年齡在18到34歲之間的女性,身高從高到矮排序, 如果身高相同的情況下按照年齡從小到大排序
select * from students where age between 18 and 34 and gender = 2 order by height desc, age desc;
      
  • 聚合函數
count(*) 以行機關來進行統計個數
 count(*) 效率更高, 效率略差:count(height)--> 擷取對應的行--> 擷取該行對應字段是否為NULL
 select count(*) from students; # 以行為統計機關
 select count(height) from students; # 以不為空的height字段對應的值作為統計機關
 ```
 - 最大值
 ```
 max()
 ```
 - 最小值
 ```
 min()
 ```
 - 求和
 ```
 sum()
 ```
 - 平均值
 ```
 avg()
 ```
 - 四舍五入
 ```
 round()
 計算所有人的平均年齡,保留2位小數
 select round(avg(age),2) from students;
      
  • 分組
分組的目的就是為了更加精細的做聚合統計操作
group by 字段1,字段2
檢視有哪幾種性别
select distinct gender from students;
select gender from students group by gender; # 将分組之後的标簽查詢出來
查詢分組資料中的人的姓名
select gender, group_concat(name) from students group by gender;
查詢同種性别中的姓名和身高
select gender,group_concat(name,":",height) from students group by gender;
查詢平均年齡超過30歲的性别,以及姓名 
select gender,group_concat(name) from students group by gender having avg(age) > 30;
having 和 where 的差別
where: 對源資料做條件篩選
having: 是對分組之後的資料做進一步的篩選操作, 有having就一定有group by, 有 group by 不一定有having
      
  • 分頁
limit start, count
start: 表示從哪裡開始查詢, start 預設值為0, 可以省略, 跳過多少條資料
count: 查詢多少條
第1頁
select * from students limit 0,4;
第2頁
select * from students limit 4,4;
第3頁
select * from students limit 8,4;
每頁顯示5個,顯示第3頁的資訊, 按照年齡從小到大排序 start = (pagenum - 1) * count
錯誤: select * from students limit 10,5 order by age asc;
正确: select * from students order by age asc limit 10,5;
      
  • 連接配接查詢
-- 連接配接查詢 将兩個表中的資料按照設定的連接配接條件進行篩選, 符合連接配接條件的資料才能夠被篩選出來
table1 inner join table2 on 設定内連接配接條件 --> 内連接配接查詢
select * from students inner join classes on students.cls_id = classes.id;
-- 按照分組的方式顯示班級的學生
select classes.name,group_concat(students.name) from students inner join classes on students.cls_id = classes.id group by classes.name;
select c.name,group_concat(s.name) from students as s inner join classes as c on s.cls_id = c.id group by c.name;
-- 外連接配接查詢: left join + right join
left join 左外連接配接查詢
-- 查詢每位學生對應的班級資訊,不滿足連接配接條件的資料會以NULL填充
select * from students left join classes on students.cls_id = classes.id;