MySQL 嵌套查询执行

发布于 2024-09-13 18:37:55 字数 84 浏览 6 评论 0原文

我正在编写一个嵌套的 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 技术交流群。

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

发布评论

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

评论(6

十二 2024-09-20 18:37:55

关于子查询返回多个值的错误告诉我,您正在尝试进行直接值比较,如下所示:

WHERE col = (SELECT col2 FROM TABLE_2)

解决方案取决于来自子查询的数据 - 您希望查询使用返回的所有值吗?如果是,则更改 IN 的等号:

WHERE col IN (SELECT col2 FROM TABLE_2)

否则,您需要更正子查询,使其仅返回一个值。 MAX 或 MIN 聚合函数是一种可能 - 它们将返回最高或最低值。这可能只是关联子查询的问题:

  FROM TABLE_1 t1
WHERE t1.col = (SELECT MAX(t2.col2)
                           FROM TABLE_2 t2
                         WHERE t2.fk_col = t1.id)  -- correlated example

正如 Tabhaza 指出的,子查询通常不会返回多于一列(尽管某些数据库支持元组匹配),在这种情况下,您需要定义派生表/内联视图并加入它。

如果能获得有关您遇到的问题的更多信息,那就太好了......

An error about a subquery returning more than one value says to me that you're attempting a straight value comparison, like this:

WHERE col = (SELECT col2 FROM TABLE_2)

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:

WHERE col IN (SELECT col2 FROM TABLE_2)

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:

  FROM TABLE_1 t1
WHERE t1.col = (SELECT MAX(t2.col2)
                           FROM TABLE_2 t2
                         WHERE t2.fk_col = t1.id)  -- correlated example

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...

メ斷腸人バ 2024-09-20 18:37:55

尝试连接到派生表而不是执行子查询;它将允许您返回多个字段:

SELECT a.Field1, a.Field2, b.Field3, b.Field4
FROM table1 a INNER JOIN 
  (SELECT c.Field3, c.Field4, c.Key FROM table2 as c) as b ON a.Key = b.Key
WHERE ...

Try joining to a derived table rather than doing a subquery; it will allow you to return multiple fields:

SELECT a.Field1, a.Field2, b.Field3, b.Field4
FROM table1 a INNER JOIN 
  (SELECT c.Field3, c.Field4, c.Key FROM table2 as c) as b ON a.Key = b.Key
WHERE ...
漫漫岁月 2024-09-20 18:37:55

这听起来像是一个逻辑问题,而不是语法问题。

为什么子查询返回不止一行?

为什么你把它放在只需要一排的地方?

你需要重组一些东西来将这两件事结合在一起。如果没有任何关于您的系统、您的查询或您的意图的指示,就很难提供进一步的帮助。

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.

迷乱花海 2024-09-20 18:37:55

如果数据库表明您返回多于一行,您应该听听它的说法并更改您的查询,以便它只返回一行。

这是你的逻辑有问题。

更改查询,使其仅返回一行。

考虑为什么查询返回多于一行,并确定如何让查询从结果中只返回您需要的一行。

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.

疯了 2024-09-20 18:37:55

在子查询上使用 LIMIT 子句,因此它始终最多返回 1 行

Use a LIMIT clause on the subquery so it always returns a maximum of 1 row

眼泪都笑了 2024-09-20 18:37:55

您可以向子查询添加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.

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