SQL ANY 查询 - 有效语法

发布于 2024-12-18 23:20:48 字数 576 浏览 3 评论 0原文

尝试使用以下“ANY”语法并收到错误:

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING EVERY (D.budget > 1000000) AND ANY (D.budget < 5000000)
             );

错误输出:

psql:queries.sql:28: ERROR:  syntax error at or near "ANY"
LINE 6:      HAVING EVERY (D.budget > 1000000) AND ANY (D.budget < 5...
                                                     ^

编辑:我正在使用 Postgres 并使用 Psql 进行连接。

Trying to use the following 'ANY' syntax and gets an error:

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING EVERY (D.budget > 1000000) AND ANY (D.budget < 5000000)
             );

Error output:

psql:queries.sql:28: ERROR:  syntax error at or near "ANY"
LINE 6:      HAVING EVERY (D.budget > 1000000) AND ANY (D.budget < 5...
                                                     ^

Edit: I am using Postgres and connecting using Psql.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情独悲 2024-12-25 23:20:48

你可以让它变得更简单,不是吗?

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 WHERE D.budget > 1000000             
                 GROUP BY D.managerid
                 HAVING MIN(D.budget) < 5000000
             );

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING EVERY (D.budget > 1000000) AND MIN(D.budget) < 5000000
             );

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING MIN (D.budget) > 1000000 AND MIN(D.budget) < 5000000
             );

You can make it simpler, no?

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 WHERE D.budget > 1000000             
                 GROUP BY D.managerid
                 HAVING MIN(D.budget) < 5000000
             );

or

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING EVERY (D.budget > 1000000) AND MIN(D.budget) < 5000000
             );

or

SELECT DISTINCT Em.ename
FROM Emp Em, Dept D
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING MIN (D.budget) > 1000000 AND MIN(D.budget) < 5000000
             );
梦魇绽荼蘼 2024-12-25 23:20:48
SELECT Em.ename
FROM Emp Em                                            --- only Emp table
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING ALL (D.budget > 1000000)       --- ALL
                   AND ANY (D.budget < 5000000)
             );
SELECT Em.ename
FROM Emp Em                                            --- only Emp table
WHERE Em.eid In (Select D.managerid 
                 FROM Dept D
                 GROUP BY D.managerid
                 HAVING ALL (D.budget > 1000000)       --- ALL
                   AND ANY (D.budget < 5000000)
             );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文