使用 SELECT 命令而不是两个 OR 条件?

发布于 2024-11-05 18:01:13 字数 557 浏览 5 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-11-12 18:01:13

应用 OR 会产生超集,而不是子集,因此您无法通过将过滤器应用于嵌套查询来获得相同的结果。

您可以使用 UNION 重写它:

SELECT  *
FROM    students
WHERE   age > 25
UNION
SELECT  *
FROM    students
WHERE   grade > C

与 相同

SELECT  *
FROM    students
WHERE   age > 25 OR grade > C

,前提是在 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:

SELECT  *
FROM    students
WHERE   age > 25
UNION
SELECT  *
FROM    students
WHERE   grade > C

is the same as

SELECT  *
FROM    students
WHERE   age > 25 OR grade > C

, provided there is a PRIMARY KEY defined on students.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文