使用 SELECT 命令而不是两个 OR 条件?
如何使用 SELECT 命令或嵌套 SELECT,而不是结合 OR 的两个条件?我知道您可以使用嵌套 SELECT 语句实现 AND,例如:
select name from (select * from students where grade > C) where age > 25
-- select name from (select * from students where grade > C) as std1 where age > 25
而不是:(
select name from students where age > 25 and grade > C
仅适用于 select 命令)。
但是如何在不使用 OR 的情况下实现 OR 条件呢?
SELECT name FROM students WHERE age > 25 OR grade > C
How can I use SELECT commands or nested SELECTs instead of two conditions combined with OR? I know you can implement AND with nested SELECT statements using, for example:
select name from (select * from students where grade > C) where age > 25
-- select name from (select * from students where grade > C) as std1 where age > 25
instead of:
select name from students where age > 25 and grade > C
(only with select command).
But how do you achieve an OR condition without using an OR?
SELECT name FROM students WHERE age > 25 OR grade > C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应用
OR
会产生超集,而不是子集,因此您无法通过将过滤器应用于嵌套查询来获得相同的结果。您可以使用
UNION
重写它:与 相同
,前提是在
students
上定义了PRIMARY KEY
。Applying
OR
results in a superset, not a subset, so you cannot achieve the same result by applying a filter to a nested query.You may rewrite it using a
UNION
:is the same as
, provided there is a
PRIMARY KEY
defined onstudents
.