SQLServer第三章 - 数据的查询(一)

第1关:基本SELECT查询

USE Mall

GO

  

SET NOCOUNT ON

  

---------- retrieving multiple column ----------

-- ********** Begin ********** --

select prod_name, prod_price from Products

  
  

-- ********** End ********** --

  

GO

  

---------- retrieving all column ----------

-- ********** Begin ********** --

select * from Products

  

-- ********** End ********** --

  

GO

第2关:带限制条件的查询和表达式查询

限制查询数量

  • MySQL 使用LIMIT
  • SQL Server 使用TOP
    表达式查询
    SELECT prod_price, prod_price*0.8 AS discount_price FROM Products
USE Mall

Go

  

SET NOCOUNT ON

  

---------- retrieving with limited ----------

-- ********** Begin ********** --

select top 2 prod_name from Products

  

-- ********** End ********** --

GO

  

---------- retrieving with expression ----------

-- ********** Begin ********** --

select prod_price, prod_price*0.8 as discount_price from Products

  
  

-- ********** End ********** --

GO

第3关:使用WHERE语句进行检索

不匹配某条件的语句

  • 用于排除多个值 not in
  • 用于排除单个值<>
    MySQL中!=<>等效,SQL Server中只能使用<>
USE Mall

Go

  

SET NOCOUNT ON

  

---------- retrieving with range ----------

-- ********** Begin ********** --

select prod_name, prod_price from Products where prod_price between 3 and 5

  
  

-- ********** End ********** --

  

GO

  

---------- retrieving with nomatches ----------

-- ********** Begin ********** --

select prod_name, prod_price from Products where prod_name <> 'Lion toy'

  
  

-- ********** End ********** --

  

GO

你可能感兴趣的:(数据库)