SQL 中的多个 WHERE 函数
我正在尝试编写一个SQL查询,以返回信息,包括产品代码,每年的销售收入,成本,销售人员来自两个不同表的信息。我需要返回“北方”地区的产品ID信息的最低登机成本。
我使用其中region ='north'
只是获取北部地区的产品信息,而 将其排序至高点并找到产品成本最低。是否有一种方法只是返回北部地区的登机成本最低的产品?
I'm trying to write an SQL query to return information including product code, yearly sales revenues, costs, sales people information from two different tables. I need to return the product ID information for the product with the lowest'onboarding cost' for the 'north' region.
I have used WHERE Region = 'North'
to just get the product info for the North region, and ORDER BY onboarding cost;
to sort this low to high and find the product with the lowest cost. Is there a way of just returning the product with the lowest onboarding cost for the north region?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 WHERE 子句中,您可以添加:
... AND onboarding cost=(SELECT MAX(onboarding cost) FROM ... WHERE region='North' ...)
但这最终会返回多个结果 - 以防出现多个结果比一种产品具有相同的最高入职成本。
In the WHERE clause you could add:
... AND onboarding cost=(SELECT MAX(onboarding cost) FROM ... WHERE region='North' ...)
But this will eventually return more than one result - in case that more than one product has the same highest onboaring cost.
是的,您需要限制记录数,如下所示:
MySQL、PostgreSQL
其中 0 是起始索引(第一行),1 是您想要获取的记录数。
SQL Server
其中
TOP 1
告诉 RDBMS 您只对第一行感兴趣。甲骨文
Yes, you need to limit the number of records, like this:
MySQL, PostgreSQL
Where 0 is the starting index (first row) and 1 is the number of records you want to get.
SQL Server
Where
TOP 1
tells the RDBMS that you are interested only in the very first row.Oracle