【sql in excel】入门17:补漏(and、or).docx
《【sql in excel】入门17:补漏(and、or).docx》由会员分享,可在线阅读,更多相关《【sql in excel】入门17:补漏(and、or).docx(6页珍藏版)》请在冰豆网上搜索。
![【sql in excel】入门17:补漏(and、or).docx](https://file1.bdocx.com/fileroot1/2022-10/1/2b9e1f7a-5446-4fae-9b3e-319a5c7a6b41/2b9e1f7a-5446-4fae-9b3e-319a5c7a6b411.gif)
【sqlinexcel】入门17:
补漏(and、or)
1、求金额小于等于200或金额大于等于500的数据。
select*from[Sheet1$]where金额<=200or金额>=500
结果:
上面的sql语句不能写为:
select*from[Sheet1$]where金额<=200or金额>=500
结果:
没有数据
虽然and可以配合where使用多个条件,但对于同一个字段的,要分清楚条件的关系。
将上述语句改为:
select*from[Sheet1$]where金额>=200and金额<=500
结果:
上述sql语句写为:
select*from[Sheet1$]where金额>=200or金额<=500
结果:
or连接的条件,只要有一个true,就能返回结果。
而and则需要每个条件都满足。
下面的sql语句将返回没有结果:
select*from[Sheet1$]where姓名='A'and姓名='B'
如改为:
select*from[Sheet1$]where姓名='A'or姓名='B'
结果:
andor结合使用。
sql语句:
select*from[Sheet1$] where姓名='A' and 金额<=200 or 金额>=500
结果:
这样看sql语句,或许有困惑。
改写为下面的格式:
select*from[Sheet1$]
where姓名='A'
and
(金额<=200)
or
(金额>=500)
这个语句分为两个条件,
第一个条件为蓝色语句,姓名A,且金额<=200,运行语句
select*from[Sheet1$]
where姓名='A'
and
(金额<=200)
结果为:
第二个条件为红色语句,金额>=500的数据,运行语句
select*from[Sheet1$]where (金额>=500)
结果为:
两者结合在一起,得出第一次的结果: