• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

不爱学习不爱运动

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

04-SqlServer高级开发2-高级数据查询

1.使用变量方式实现查询

use StudentManageDB
go

--定义变量
declare @stuId int 
--获取学号
select @stuId = StudentId from Students where StudentName='张永利';

select * from Students where StudentId > @stuId;

 2.示例2:简单子查询

---- 示例2:简单子查询 ----------------------
use StudentManageDB
go

--查询学号排在张永利后面的学员
select * from Students where StudentId>(select StudentId from Students where StudentName='张永利')

3.使用等值连接查询

-- 3.使用等值连接查询
select * from Students 
inner join ScoreList
on Students.StudentId=ScoreList.StudentId
where SQLServerDB>70

4.in 子查询的使用

--4.in 的子查询的使用
select  * from Students 
--where StudentId =
--(select StudentId from ScoreList where SQLServerDB>80)  --这是不允许的

where StudentId in
(select StudentId from ScoreList where SQLServerDB>80) 

5.not in的使用

select * from Students 
where StudentId not in (select StudentId from ScoreList)

6.EXISTS的使用

--6. EXISTS的使用

if exists(select * from ScoreList WHERE CSharp<60)
    print '本次考试内容较难'
else
   print '本次考试内容适中'


-- not exists 的使用
if not exists(select * from ScoreList where CSharp<60)
      print '本次考试内容适中'
else
      print '本次考试内容较难'

posted on 2020-08-24 16:03  不爱学习不爱运动  阅读(111)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3