MySQL常用語句之查詢(一)

MySQL數據查詢在工作中使用的是最多的,本篇專門記錄數據庫查詢的各種招式和技巧。

  • 基本查詢
  • 條件查詢
  • 查詢排序
  • 聚合函數
  • 分組查詢
  • 分頁查詢
  • 連接查詢
  • 子查詢
  • 自關聯

基本查詢

1. 查詢所有字段(生成環境慎用)
	select * from student;


2. 查詢指定字段
	select name, age from student;
	select student.name, student.age from student;


3. 使用`as`給字段設置別名	
	select name as "名字", age as "年齡" from student;


4. 使用`as`給表設置別名
	select s.name, s.age from student as s;


5. 使用`distinct`消除重複行
	select distinct name from student;

條件查詢

1. 比較運算符(`>``>=``<``<=``= ``!=`-- 查詢大於18歲的信息
	select * from student where age > 18;
	
	-- 查詢大於等於18歲的信息
	select * from student where age >= 18;
	
	-- 查詢 id 小於50的信息
	select * from student where id < 50;
	
	-- 查詢 id 小於等於50的信息
	select * from student where id <= 50;
	
	-- 查詢 id = 10 的信息
	select id, name, age from student where id = 10;
	
	-- 查詢名字不等於 “張三” 的學生信息
	select id, name, age from student where name != '張三';



2. 邏輯運算符(`與:and``或:or``非:not`-- 查詢 18歲 到 25歲 之間的員工信息
	select * from staff where age > 18 and age < 25;
	
	-- 查詢 18歲以上男員工的信息
	select * from staff where age > 18 and sex = "男";
	
	-- 查詢 20歲以上 或 工資6000(包含)以上的員工信息
	select * from staff where age > 20 or salary >= 6000;
	
	-- 查詢 不大於50歲 的女員工信息 "()"控制優先級
	select * from staff where not (age > 50 and sex = "女");
	
	-- 查詢 年齡不大於20歲 工資大於6000的員工
	select * from staff where (not age > 20) and salary > 6000;

	

3. 模糊查詢(`like``%``_``rlike`-- 查詢 名字中以 “王” 開頭的信息
	select * from student where name like "王%";
	
	-- 查詢 名字中以 “平” 結尾的信息
	select * from student where name like "%平";
	
	-- 查詢 名字中有 “王” 的信息
	select * from student where name like "%王%";
	
	-- 查詢 兩個字的名字信息
	select * from student where name like "__";
	
	-- 查詢 至少兩個字的名字信息
	select * from student where name like "__%";

	-- 查詢 以“王”開始的名字
	select name from student where name rlike "^王.*";
	
	-- 查詢 以“王”開頭,“平”結尾的名字
	select name from student where name rlike "^王.*平$";


 
4. 範圍查詢()

`in` :表示在一個非連續的範圍內
`not in` :不非連續的範圍內
`between...and...` :表示在一個連續的範圍內
`not between...and...` :表示在一個連續的範圍內

	-- 查詢 年齡爲18、30的學生
	select * from student where age = 18 or age = 30;
	select * from student where age in (18,30);
	
	-- 查詢 年齡不是18、30的學生
	select * from student where age not in (18,30);
	
	-- 查詢 年齡是18到30之間的學生
	select * from student where age between 18 and 30;
	
	-- 查詢 年齡不是18到30之間的學生
	select * from student where age not between 18 and 30;



5. 判斷空值

is null: 等於空值
is not null: 不等於空值

	-- 查詢 content爲空的信息
	select * from heroes where content is null;
	-- select * from heroes where content = null 無結果;
	
	-- 查詢 content不爲空的
	select * from heroes where content is not null;
	-- select * from heroes where content != null 無結果;
	

查詢排序

order by 字段
asc 升序(默認值)
desc 降序
order by 多個字段

	-- 查詢 年齡在18-22之間的女學生,按照年齡升序排列
	select * from student where (age between 18 and 22) and sex = "女" order by age; 
	select * from student where (age between 18 and 22) and sex = "女" order by age desc; 

	-- 查詢 年齡在16-20之間的男生,按身高降序排列,如相同,按年齡升序排列
	select * from student (age between 16 and 20) and sex = "男" order by height desc,age asc;

	-- 查詢 所有學生,按年齡小到大排列,身高從高到低
	select * from student order by age, height desc;

聚合函數

count(*) -- 總數
max() 	 -- 最大值
sum()    -- 求和
avg()    -- 平均值

	-- 查詢 所有學生總數
	select count(*) as "學生總數" from student;

	-- 查詢 最大的年齡
	select max(age) from student;

	-- 查詢 男生最高分數
	select max(score) as "最高分數" from student where sex = "男";

	-- 查詢 所有人分數總和
	select sum(score) from student;

	-- 查詢 男生平均分數
	select avg(score) from student where sex="男";
	select sum(score)/count(*) from student where sex="男";


分組查詢

group by: 分組
group_concat():  會計算哪些行屬於同一組,將屬於同一組的列顯示出來,配合group by使用
having:過濾分組
with rollup: 實現在分組統計數據基礎上再進行相同的統計(SUM,AVG,COUNT…)。
coalesce: 來設置一個可以取代 NUll 的名稱

	-- 按照性別分組,查詢所有的性別
	select sex from student group by sex;
 
 	-- 查詢每種性別各多少人
 	select sex, count(*) as '數量' from student group by sex;

	-- 查詢每組中的最高分數、平均分數
	select sex, max(socre) from student group by sex;
	select sex, avg(socre) from student group by sex;

	-- 查詢每組中的包含分數信息
	select sex, GROUP_CONCAT(socre) from student group by sex;

	-- 計算男生的人數
	select sex, count(*) from student where sex="男" group by sex;

	-- 計算男生的人數,並查看男生名字
	select sex, count(*), group_concat(name) from student where sex="男" group by sex;
	
	-- 計算男生的人數,並查看男生名字,以及每個人的分數: 名字__90
	select sex, count(*), group_concat(name, score) from student where sex="男" group by sex;
	select sex, count(*), group_concat(name,'__',score) from student where sex="男" group by sex;

	-- 查詢人數多於2的性別
	select sex, group_concat(name) as '名字',count(*) from student group by sex having count(*) > 2;
	
	-- 分別計算男、女平均分,再計算總人數的平均分	
	select sex, avg(score) from student group by sex with rollup;
	-- 上面的結果總平均數 KEY值是:NULL,使用coalesce 替換 'NULL' 爲 '總平均數'
	select coalesce(sex, '總平均數'), avg(score) from student group by sex with rollup;

在這裏插入圖片描述

分頁查詢

  • limit start(起始), count(個數)
-- 限制查詢 10個學生
select * from student limit 10;

-- 查詢前 20個學生
select * from student limit 0, 20;

-- 查詢所有男生,按分數降序排列,只顯示前 10名
select * from student where sex = '男' order by score desc limit 10;

連接查詢

  • inner join ... on 內連接:取多個表的交集
  • left join ... on 左連接:取左表所有記錄,即使右表沒有對應匹配的記錄
  • right join ... on右連接:取右表所有記錄,即使左表沒有對應匹配的記錄
-- 查詢 表1中與表2中名字相同名字的記錄( inner 可以省略)
select a.name, a.age, a.sex from table1 a join table2 b on a.name = b.name;

-- 查詢 表1中所有的記錄
select a.name, a.age, a.sex from table1 a left join table2 b on a.name = b.name;

-- 查詢 表2中所有的記錄
select a.name, a.age, a.sex from table1 a right join table2 b on a.name = b.name;

子查詢

  • slelect 內嵌 select
-- 查詢最高分的學生的信息
select * from student where score = (select max(score) from student);

自關聯

  • 一個表通過更改別名,當做兩個表使用
select * from1 as 表A inner join1 as 表B on 表A.xxx=表B.yyy having 條件;

THE END !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章