如何选择最高总值的对象组
我有三张表格:
对象(numId is primary键)
object_category | 价格 | 1 |
---|---|---|
永久 | 20 | 2 |
借用 | 30 | 3 |
借用 | 10 | ( |
primary键)
is em |
---|
colname |
numId |
借用
(numobj是 emere键/主键参考对象表, collection_name is em efiral键 rector collection表)
numobj | collection_name |
---|---|
1 | collection_alpha |
2 | collection_betha |
3 | 我需要做的查询收集 |
是:“哪些集合(不包括永久性)的借入对象总成本最高?”
我目前的尝试(无效):
SELECT ColName FROM Colecao
WHERE (
SELECT MAX((SUM(Price)) FROM Objects AS Num
JOIN Borrowed ON NumObj = Objects.NumId
JOIN Collection ON ColName = Collection_name
WHERE Num > COUNT(NumId) FROM Objects
WHERE Object_category = "permanent"
);
它返回消息:“语法错误”
I have three tables like this:
Objects (NumId is Primary key)
NumId | Object_category | Price |
---|---|---|
1 | permanent | 20 |
2 | borrowed | 30 |
3 | borrowed | 10 |
Collection (ColName is Primary Key)
ColName |
---|
collection_alpha |
collection_betha |
Borrowed
(NumObj is Foreign Key/Primary Key referring Object table, and Collection_name is Foreign Key referring Collection table)
NumObj | Collection_name |
---|---|
1 | collection_alpha |
2 | collection_betha |
3 | collection betha |
The query I need to do is: "Which collections (excluding the permanent) have the highest total cost of borrowed objects?"
my current attempt (that doesn't work):
SELECT ColName FROM Colecao
WHERE (
SELECT MAX((SUM(Price)) FROM Objects AS Num
JOIN Borrowed ON NumObj = Objects.NumId
JOIN Collection ON ColName = Collection_name
WHERE Num > COUNT(NumId) FROM Objects
WHERE Object_category = "permanent"
);
It returns message: "syntax error"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为您列出所有按借入的对象的总体价值下降的收集列表。您可以添加
限制1
以显示列表的顶部项目。但是,想象一下,有两个收藏恰好具有相同的总价值。他们将 都具有“借入对象的最高总成本” ,因此应退还两者。上面具有
限制1
的查询将省略其中之一。在这种内等级= 1。
或者,我们可以设置 cte 并表达了类似的东西:
gives you a list of all collections ordered descending by the overall worth of the borrowed objects. You could add
limit 1
to show the top item of the list.But imagine there are two collections that happen to have the same total worth. They would both have "the highest total cost of borrowed objects", so both should be returned. The above query with
limit 1
would omit one of them.In this case we could rank the collections according to their worth, and return all rows with rank = 1.
Alternatively, we could set up a CTE and express the same thing like this: