數據庫的單標查詢 DQL

DQL: 查詢表中的記錄

1)語法:
	select * from 表名;

	select 
		字段名列表
	from
		表名列表
	where
		條件列表
	group by
		分組條件
	order by
		排序
	limit
		分頁限定

2)基礎查詢
	(1)多個字段的查詢
		select name, age from student;
		select * from student;  --不方便閱讀,一般全部名字寫出來


		select 
			name, 	 --姓名
			age, 	 --年齡
		from 
			student; --學生表

	(2)去除重複
		select distinct address from student;  --要去重,必須保證結果集一樣
		select distinct name, address from student; --去重不掉,因爲結果集不完全相同

		
	(3)計算列
		select name, math, english, math+english from student; -- math和英語之和

	(4)起別名
		select name, math, english, math+ ifnull(english, 0) as 總分 from student; -- math和英語之和

3)條件查詢
	(1)where字句後跟條件
		select * from student where age > 20;
		select * from student where age >= 20;

		select * from student where age = 20;

		select * from student where age != 20;

		select * from student where age <> 20;

		select * from student where age >= 20 && age <= 30;
		select * from student where age >= 20 and age <= 30;

		select * from student where age = 20 or age = 30;
		select * from student where age in(22,18,25);

		注意: select * from student where english = null; -- 是不對的,查詢不出來
		select * from student where english is null; --查詢缺考的
		select * from student where english is not null; --不爲null的

		select * from student where name like '馬%'; 
		select * from student where name like '_化%'; 
		select * from student where name like '___%'; 
		select * from student where name like '%馬%'; 
			_: 單個字符
			%:多個字符

	(2)運算符
		> < <= >= = <>
		between ... and
		in(集合)
		like
		is null
		and 或 &&
		or 或 &&
		not 或 !

4)查詢語句
	(1)排序查詢 order by 子句;
		order by 排序字段1 排序方式1 排序字段2 排序方式2...;
			如: 
				默認是升序排列: select * from order by math;
				升序: select * from order by math asc;
				降序: select * from order by math desc;

				先按數學排名,如果數學一樣,則按照英語排名(第2排序):	select * from order by math asc, english asc;

	(2)聚合函數:將一列的數據作爲一個整體,進行縱向的計算
		count: 計算個數
			select count(name) from student; -- 查詢有幾個學生. 單行單列。 排除了null的值了
			select count(isnull(english, 0)) from student; -- 如果是null,則被替換爲0

			select count(*) from student;
		max: 計算最大值
			select MAX(math) from student;

		min: 計算最小值
			select min(math) from student;

		sum: 求和
			select sum(math) from student;

		avg: 計算平均值
			select avg(math) from student;

	(3)分組查詢(每一組的平均分更高一點,把一組的同學當成一個整體來看,把相同屬性的當成一個組來看待)
		如: 按照男、女來分爲2組計算下平均分:
			select sex, avg(math), count(id) from student group by sex;
				女 91 3
				男 72 5

		注意:分組之後,除了分組的字段,不要加其他字段了
			如:select name, sex, avg(math), count(id) from student group by sex; -- 這個name就不要加,沒意義

		分數低於70分的不參與分組(認爲不是這個班的學生): select sex, avg(math), count(id) from student where math > 70 group by sex;

		分組前對條件做一些限定,如: 邀請人數要大於2人
			select sex, avg(math), count(id) from student where math > 70 group by sex having count(id) > 2;
			優化寫法(起別名,拿別名判斷):
				select sex, avg(math), count(id) 人數 from student where math > 70 group by sex having 人數 > 2;

		面試: where和having的區別:
			where在分組之前進行限定,如果不滿足條件,則不參與分組;
			having在分組之後限定,如果不滿足結果,則不會被查詢出來;

	(4)分頁查詢(查詢結果有300W個,就可以搞幾頁)
		語法: limit 開始的索引,每頁查詢的條數

		例子: 每頁顯示3條記錄
			select * from student limit 0, 3; -- 從0開始查詢3條記錄 第1頁 
			select * from student limit 3, 3; -- 從0開始查詢3條記錄 第2頁

		公式: 開始索引 = (當前頁碼-1) * 每頁顯示的條數
			select * from stu limit (當前頁碼-1) * 每頁顯示的條數, 每頁顯示的條數

		分頁操作,是一個方言。 limit只能在mysql中用,在oracle等都有自己各自的寫法.


5)約束

6)多表之間的關係

7)範式: 抽取表的設計規則, 

8)數據庫的備份與還原

 

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