單表查詢DQL

基本數據檢索:單表正則表達式

複雜數據檢索:多表:鏈接查詢、子查詢(嵌套查詢)、集合運算函數

 

 

 

基本select語句:

  • select <檢索字段>
  • from <表>
  • where <檢索條件>
  • group by<分類>
  • having<檢索條件>
  • order by <排序字段>

操縱列:

  • 1.輸出全部列:select *
  • 2.輸出指定列:select <字段>[,...n]
  • 3.計算表達式:select <表達式>[,...n]
  • 4.設置列表標題名:<表達式> [AS] <別名>|<別名>=<表達式>
  • 5.消除重複記錄:distinct

 

 1 select * from <表名>     --查詢表中全部數據
 2 
 3 select <字段名>,<字段名>  form <表名>   --投影
 4 
 5 select <表達式> from <表名>  --查詢計算列
 6 --eg:表達式爲:2020-sage sage爲字段名
 7 --:select 2020-sage from 表名
 8 
 9 --計算列沒有名稱,一般須要 命別名
10 --1.字段 as 別名 :  select 2020-sage as 別名 from 表名
11 --2.字段 別名,即as 可省: select 2020-sage 別名 from 表名
12 --3.別名=字段: select 出身年=2020-sage from 表名
13 
14 select [謂詞] 字段 from 表名 15 --1. distinct 去重 : select distinct 2020-sage as 別名 from 表名

 

操做行spa

1.普通查詢:where <邏輯表達式>code

2.模糊查詢:1. 運算符 like   2.通配符 :%任意個字符,_任意一個字符orm

select [謂詞] 字段 from 表名 --2.top n:查詢記錄的前n行
select top 3 * from 表名     --選擇前 n 行 --3.top n percent :查詢前n%行
select top 3 percent * from 表名     --選擇前 n% 行

select top n percent 字段 fromwhere 表達式 order by 排序字段名 [asc]/desc
--order 默認的排序方式是升序asc,可不寫

select top n percent with ties 字段 fromwhere 表達式 order by 排序字段名 [asc]/desc
--with ties 顯示排序字段的並列值 --eg: top 3 :但第三名與第四名排序字段相同,則with ties 使第三名和第四名都顯示出來

--in /not in (子查詢/表達式列表) :過濾記錄
select  * from 表名 where grade in (88,99) --between/not between 起始值 and 終止值 :過濾記錄
select  * from 表名 where grade between 80 and 90

--字段 like '正則表達式' :模糊匹配
select * from where 學號 like '%[1,4]'  --匹配以1,或4結尾的學號

 

 

 

分組查詢

group by 分組字段

 

聚合函數blog

 

 

select count(字段名)  fromgroup by 分組字段   --查找每一個分組的記錄數量
--當使用 count(*)時,統計全部記錄
--當使用 count(字段名)是,統計記錄不包含null
--當使用 count(distinct 字段名)時,統計記錄不包含重複和null

 

若分組增長條件則使用 having,可在彙總後過濾排序

即,分組以前的條件使用where ,分組以後的條件使用havingit

相關文章
相關標籤/搜索