MySQL 嵌套查询执行
我正在编写一个嵌套的 MySQL 查询,其中子查询返回多于一行,因此该查询无法执行。
谁能建议我解决这个问题?
提前致谢。
I am writing a nested MySQL query where a subquery returns more than one row and hence the query can not be executed.
Can anyone suggest me a solution for this problem?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
关于子查询返回多个值的错误告诉我,您正在尝试进行直接值比较,如下所示:
解决方案取决于来自子查询的数据 - 您希望查询使用返回的所有值吗?如果是,则更改 IN 的等号:
否则,您需要更正子查询,使其仅返回一个值。 MAX 或 MIN 聚合函数是一种可能 - 它们将返回最高或最低值。这可能只是关联子查询的问题:
正如 Tabhaza 指出的,子查询通常不会返回多于一列(尽管某些数据库支持元组匹配),在这种情况下,您需要定义派生表/内联视图并加入它。
如果能获得有关您遇到的问题的更多信息,那就太好了......
An error about a subquery returning more than one value says to me that you're attempting a straight value comparison, like this:
The solution depends on the data coming from the subquery - do you want the query to use all the values being returned? If yes, then change the equals sign for an IN:
Otherwise, you need to correct the subquery so it only ever returns one value. The MAX or MIN aggregate functions are a possibliity - they'll return the highest or lowest value. It could just be a matter of correlating the subquery:
As Tabhaza points out, a subquery generally doesn't return more than one column (though some databases support tuple matching), in which case you need to define a derived table/inline view and join to it.
Would've been nice to have more information on the issue you're having...
尝试连接到派生表而不是执行子查询;它将允许您返回多个字段:
Try joining to a derived table rather than doing a subquery; it will allow you to return multiple fields:
这听起来像是一个逻辑问题,而不是语法问题。
为什么子查询返回不止一行?
为什么你把它放在只需要一排的地方?
你需要重组一些东西来将这两件事结合在一起。如果没有任何关于您的系统、您的查询或您的意图的指示,就很难提供进一步的帮助。
this sounds like a logic problem, not a syntax problem.
why is the subquery returning more than one row?
why do you have that in a place that requires only one row?
you need to restructure something to fit these two things together. without any indication of your system, your query, or your intent, it is very hard to help further.
如果数据库表明您返回多于一行,您应该听听它的说法并更改您的查询,以便它只返回一行。
这是你的逻辑有问题。
更改查询,使其仅返回一行。
考虑为什么查询返回多于一行,并确定如何让查询从结果中只返回您需要的一行。
If the database says you are returning more than one row, you should listen to what it says and change your query so that it only returns one row.
This is a problem in your logic.
Change the query so that it only returns one row.
Think about why the query is returning more than one row, and determine how to get the query to return just the single row you need from that result.
在子查询上使用 LIMIT 子句,因此它始终最多返回 1 行
Use a LIMIT clause on the subquery so it always returns a maximum of 1 row
您可以向子查询添加
LIMIT 1
,以便顶级查询仅考虑第一个结果。您还可以在执行 LIMIT 之前对子查询的结果进行排序,以返回具有最高/最低 X 的结果。但请确保这实际上是您想要发生的情况,因为多行子查询通常是底层子查询的症状问题。You could add a
LIMIT 1
to the subquery so the top query only considers the first result. You can also sort the results from the subquery before doing the LIMIT, to return the result with the highest/lowest X. But make sure that that's actually what you want to happen, as the multi-row subquery is often a symptom of an underlying problem.