子查询

方法一 示例1变量版

use xuexioa  --切换到xuexiao的数据库

declare @no int --声明一个整型局部变量@no

select @no=stuNo from student where stuName='张三' --查询出张三的学号赋值给@no

select*from student where stuNo>@no --查询出学号比张三的学号大的学生

方法二  示例2子查询版

select*from student where stuNo>  --查询出比张三学号大的学生

(select stuNo from student where stuName='张三') --小括号中子查询:查询张三的学号

--子查询语句必须防止在一对圆括号内,外面的查询成为父查询,括号中的查询称为子查询

--执行时,先执行子查询部分,求出子查询部分的值,在执行整个父查询,返回最后的结果

--子查询作为where条件的一部分,还可以和update,insert,delete一起使用

原文地址:https://www.cnblogs.com/xiaobaicom/p/6565021.html