您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql select 结合where子语句查询结果集

2024/6/1 17:11:25发布25次查看
where子句在select语句中,语句首先从from子句开始执行,执行后会生成一个中间结果集,然后就开始执行where子句。where子句是对from子句生成的结果集进行过滤,对中间结果集的每一行记录,where子句会返回一个布尔值,(true/false),如果ture,这行记录继续留在结果集中,如果false,则这行记录从结果集中移除。如:
select name from student where studentno = 2
from子句返回的中间结果集如下:
studentno  name1  张三2  李四3  王五4  赵六
总共4行记录,对每一行记录执行where子句。第一行中studentno是1,所以studentno=2表达式返回值为false,这行记录移除。第二行中studentno是2,所以studentno=2返回true,这行记录继续保留;同理第三行和第四行记录也移除,执行完where语句后的中间结果集为:
studentno  name2  李四
然后执行select语句,最终的结果集为:
name李四
比较运算符where子句返回布尔值,所以where子句经常会用到比较运算符。比较运算符有:
=  等于
相等或者都等于空
> 大于
>= 大于或等于
不等于
!= 不等于
2=2的结果为true,152的结果为true,5!=4的结果为true。
字符串也可以进行比较,'b''k'的结果为false。
时间值可以比较,较早的时间小于较晚的时间。'1980-5-4''1991-2-19'的结果为false。
=比较符与比较符的差别在于,当比较两个空值的时候,=返回unknown,返回为true。
子查询中的比较运算符select studentno from student where studentno > (select studentno from student where name='李四')
一个子查询可以用于where子句中。上例中是一个标量子查询,子查询只能返回一个标量值。
同样一个行子查询也可以用于where子句中:
select studentno from student where (studentno,name) = (select studentno,name from student where name='李四')
where子句并不一定带比较运算符,当不带运算符时,会执行一个隐式转换。当0时转化为false,当其他值是转化为true。
不带比较运算符的where子句select studentno from student where 0
则会返回一个空集,因为每一行记录where都返回false。
select studentno from student where 1
或者
select studentno from student where 'abc'
都将返回student表所有行记录的studentno列。因为每一行记录where都返回true。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product