SQL Server 查询基础
生活随笔
收集整理的這篇文章主要介紹了
SQL Server 查询基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常用的行聚合函數和功能
| count(*) | 返回數據中的項數 |
| count({ [ [ all或distinct ] 列名 ] } ) | 返回某列的個數 |
| avg({ [ [ all或distinct ] 列名 ] } ) | 返回某列的平均值 |
| max({ [ [ all或distinct ] 列名 ] } ) | 返回某列的最大值 |
| min({ [ [ all或distinct ] 列名 ] } ) | 返回某列的最小值 |
| sum({ [ [ all或distinct ] 列名 ] } ) | 返回某列值得和 |
基本表 Employee
create table Employee( Id int, Name varchar(20), Sex varchar(20), Age int ) insert into Employee values (001,'張子婷','女',24), (002,'你愁啥','男',26), (003,'黑客','女',29), (004,'大佬','男',20), (005,'尼奧','女',24), (006,'安德森','男',15)1. 別名查詢
別名的 3 中定義方法:
- 別名=列名
- 列名 as 別名
- 列名 別名
2. into 子句
創建新表并將來自查詢的結果行插入新表中。
[into new_table]eg. 使用 into 子句創建一個新表 tb_Employ, tb_Employ表中包含 Employ 表中的Name和Age字段。
select Name, Age into tb_Employe from Employeeselect * from tb_Employe3. where子句
指定查詢返回的行的搜索條件。
(1)邏輯運算符(not、and、or)
select * from Employe where Sex='女' and Age =20 select * from Employe where Id='001' or Id='002' select * from Employe where Sex='男' and not Age>20 select * from Employe where Sex='男' and Age=20 or Sex='女' and Age='23'(2) 比較運算符
select * from Employe where Name='張子婷' select * from Employe where Age>24(3)like 關鍵字
使用 like 關鍵字可以確定特定字符是否與指定模式相匹配。模式可以包含常規字符和通配符。模式匹配過程中,常規字符必須與字符串中指定的字符完全匹配。但是,通配符可以與字符串的任意部分相匹配。
| % | 包含零個或多個字符的任意字符串 | |
| _ (下劃線) | 任何單個字符 | |
| [ ] | 指定范圍([a~f])或集合([abcdef])中任何單個字符 | |
| [^] | 指定范圍([a~f])或集合([abcdef])中任何單個字符 |
總結
以上是生活随笔為你收集整理的SQL Server 查询基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html前端 echarts图表使用详解
- 下一篇: python遍历字典方法总结