and 和 or 运算符用于基于一个以上的条件对记录进行过滤。
and 和 or 运算符
and 和 or 可在 where 子语句中把两个或多个条件结合起来。
如果第一个条件和第二个条件都成立,则 and 运算符显示一条记录。
如果第一个条件和第二个条件中只要有一个成立,则 or 运算符显示一条记录。
原始的表 (用在例子中的)
lastname | firstname | address | city |
---|---|---|---|
adams | john | oxford street | london |
bush | george | fifth avenue | new york |
carter | thomas | changan street | beijing |
carter | william | xuanwumen 10 | beijing |
and 运算符实例
使用 and 来显示所有姓为 "carter" 并且名为 "thomas" 的人:
select * from persons where firstname='thomas' and lastname='carter'
结果:
lastname | firstname | address | city |
---|---|---|---|
carter | thomas | changan street | beijing |
or 运算符实例
使用 or 来显示所有姓为 "carter" 或者名为 "thomas" 的人:
select * from persons where firstname='thomas' or lastname='carter'
结果:
lastname | firstname | address | city |
---|---|---|---|
carter | thomas | changan street | beijing |
carter | william | xuanwumen 10 | beijing |
结合 and 和 or 运算符
我们也可以把 and 和 or 结合起来(使用圆括号来组成复杂的表达式):
需要了解更多数据库技术:SQL AND & OR 运算符,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!
select * from persons where (firstname='thomas' or firstname='william') and lastname='carter'
结果:
lastname | firstname | address | city |
---|---|---|---|
carter | thomas | changan street | beijing |
carter | william | xuanwumen 10 | beijing |
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/dtteaching/625844.html