SQL__查询语句之单表查询

SQL__查询语句之单表查询

1.检索特定属性

  • 举例说明

    
    	USE AdventureWorks
    
    	SELECT  DepartmentID,Name
    	FROM HumanResources.Department 
    	
    
  • 更改列名为指定的列名:共有三种方式

    
    	SELECT 'Department number'= DepartmentID 
    	FROM HumanResources.Department
    	
    	SELECT DepartmentID 'Departmen Number' 
    	FROM HumanResources.Department
    	
    	SELECT DepartmentID AS 'Department Number'
    	FROM HumanResources.Department 
    	
    	SELECT DepartmentID AS 'Department Number'
    	FROM HumanResources.Department 
    	
    
  • SQL查询从Employee表和文字"Designation"中检索雇员ID及其头衔

    	
    	-----文字"Designation"是为了使结果更具有说明性,可以是任意文字,所添加的列名作为虚拟列名
    	-----用法:'文字:'作为列名来使用
    	SELECT EmployeeID,'Designation:',Title  
    	FROM HumanResources.Employee
    	
    	
    	----------/补充(较少用)/------------
    	--并置字符串:SELECT '列名1'+'列名2'+---+'列名n'
    	--用于满足用户的多种需求,提高输出程序的可读性
    	SELECT 'Name'+'GroupName'+'DepartmentID'as Department
    	FROM HumanResources.Department
    	
    	
    	--计算列值:+  -  *  /  %和java中的运算法则一样,可以添加括号()来更改优先级。
    	--优先级相同时,运算法则为从左到右
    	SELECT SalesOrderID,Unitprice,OrderQty,Unitprice*OrderQty as PRICE_SUM
    	FROM Sales.SalesOrderDetail
    		
    

2.检索选定行

  • 例子如下
    
    	----关键字: where + 条件 
    	----top 2 *表示:显示整个表格中的前两个字段(记录)  
    	select top 2 * from HumanResources.Employee
    	where VacationHours<5
    	----where 直接和列挂钩 即:where + 列名(判断)
        
    

3.这里查看选定的记录是否存在表中

  • 例子如下

    
    	--------'*'表示全选------
    	--------查看表中是否有叫'Production Teachnician-WC60'的Title,是否有叫做'M'的 MaritalStatus.
    	----有两种方法:"="和"in / not in"
    	----这里的M和Production Teachnician-WC60是列中的属性值*/
    	
    	select * from HumanResources.Employee
    	where Title ='Production Teachnician-WC60' and  MaritalStatus ='M' ---  ( =int(数字) 或 = 'String'(字符串))
    	----或者-----
    	select *from HumanResources.Employee
    	where Title in ('Production Teachnician-WC60')and MaritalStatus in('M')
    	
    

4.查询某一列或多列中是否存在想查询的属性(属性值):

  • 例子如下

    
    	--列名 Not in('属性值1','属性值2''属性值n等') 或者 列名 in('',''等)
    	select EmployeeID,Title,LoginId 
    	from HumanResources.Employee 
    	where  Title IN('Recruiter','Stocker','Buyer')
    	
    	select EmployeeID,Title,LoginID 
    	from HumanResources .Employee 
    	where Title not IN('0','buyer')
    	
    	
    	/*sth between 常量、变量、函数、表达式 and 常量、变量、函数、表达式 */
    	--表达式、函数都可以和列名联系起来做相应的变换运算来查询相关的数据信息
    	select EmployeeID,VacationHours 
    	from HumanResources.Employee
    	where VacationHours not between 20 and 50
    	
    

5.通配符:* _ [] [^]

  • []:指定范围内的单个字符

  • [^]:指定范围外的单个字符


  • 例子如下

    
    	USE AdventureWorks
    	GO
    	--查询Name列的值以"Pro"开头的记录使用通配符"%"来查询
    	SELECT * 
    	FROM HumanResources.Department
    	WHERE Name Like 'Pro%'
    	
    	--检索部门名称为5个字符且最后一个字符为空的情况
    	SELECT  *
    	FROM HumanResources.Department 
    	WHERE Name like 'Sale_'
    
    

6.补充说明( 模糊查询 )

  • LIKE 'LO%’ :表示以"LO"开头的所有名称
  • LIKE ‘%LO’ :表示以"LO"结尾的所有名称
  • LIKE ‘%LO%’ :表示包含字母"LO"的所有名称
  • LIKE ‘_LO’ :所有以"LO"结尾的所有名称
  • LIKE ‘[LO]%’ :以"L"或"O"开头的所有名称
  • LIKE ‘[L—O]ear’:整个字符串一共有四个字符,表示以"ear"结尾并以"L"到"O"中任一字母开头的四个字母的名称
  • LIKE ‘D[^c]%’ :整个字符串一共有两个字符,表示以"D"开头并且第二个字母不是"c"的所有名称
  • 如果遇到更加复杂的查询,在此基础上做出相应的变换即可

7.检索包含 NULL 或者 NOT NULL 值的记录

  • 例子如下
    
    	SELECT EmployeeID,EndDate 
    	FROM HumanResources.EmployeeDepartmentHistory
    	WHERE EndDate is NUll
    	
    	SELECT EmployeeID,EndDate 
    	FROM HumanResources.EmployeeDepartmentHistory
    	WHERE EndDate is NOT NUll
    	
    	
    	-------------检索并更换NULL为指定的数字、表达式、函数
    	SELECT ContactID,MiddleName,Suffix,isnull(MiddleName,100)as 更改后的值,isnull(Suffix,10)as 数据 
    	FROM Person.Contact
    	--语法:isnull(列名,想要更改的值)
    
    
    	
    	----Top--顶部关键字---
    	select top 10 * 
    	from HumanResources.Employee
    	
    	----检索某表的前 n(10%) 行---
    	select top 10 percent *
    	from HumanResources.Employee
    
    

8.检索某表中指定列的前 n(10) 行

  • 例子如下
    
    	----约束名:top n with ties 列名
    	----select top n with ties 列名----
    	select top 10 with ties EmployeeID,Title,SickleaveHours
    	from HumanResources.Employee
    	Order by SickLeaveHours desc
    	
    

9.从特定位置检索记录

  • 例子如下
    
    	----关键字:OFFSET (排除记录)---
    	----offset 只可以与 fetch 一起使用---
    	select EmployeeID,NationalIDNumber,ContactID,HireDate
    	from HumanResources.Employee 
    	offset 15 rows
    	
    

10.检索不重复值的记录

  • 例子如下
    
    	------------------关键字:distinct(不重复的)--------
    	select distinct Title  
    	from HumanResources.Employee 
    	Where Title LIKE 'PR%'
    			
    

11.补充说明:关键字 :offset 和 fetch(较为少见)

  • 例子如下
    
    	----检索10条记录,排除前15条记录,这时候需要用到 offset 和 fetch 关键字
    	/* offset子句 和 fetch子句 要和Order by子句一起使用 */
    	----检索10条记录,排除前15条记录,这时候需要用到 offset 和 fetch 关键字
    	select EmployeeID,NationalIDNumber,ContactID,HireDate
    	from HumanResources.Employee 
    	Offset 15 rows        /*排除前15条记录*/
    	fetch next 10 rows only /*检索出来的是从第16条开始的记录*/
    
    
发布了56 篇原创文章 · 获赞 51 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43495629/article/details/104212587