mysql,使用两个select

发布于 2024-09-13 03:13:28 字数 185 浏览 3 评论 0原文

为什么我收到错误: 子查询返回超过 1 行

 SELECT name, cat_id,
    (
    SELECT topic
    FROM category
    WHERE cat = u.cat_id
    ) AS topics
    FROM name u

谢谢

why i get error:
Subquery returns more than 1 row

 SELECT name, cat_id,
    (
    SELECT topic
    FROM category
    WHERE cat = u.cat_id
    ) AS topics
    FROM name u

Thanks

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

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

发布评论

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

评论(9

绝對不後悔。 2024-09-20 03:13:28

我可能有一个愚蠢的答案,但你为什么不使用 JOIN ?

SELECT name.cat_id, name.name, category.topic
FROM name INNER JOIN category 
ON category.cat = name.cat_id

I have maybe a silly answer but why arent't you using a JOIN ?

SELECT name.cat_id, name.name, category.topic
FROM name INNER JOIN category 
ON category.cat = name.cat_id
好菇凉咱不稀罕他 2024-09-20 03:13:28

子查询:

SELECT topic
FROM category
WHERE cat = u.cat_id

返回多个结果 - 您试图将其放入一行。

The sub-query:

SELECT topic
FROM category
WHERE cat = u.cat_id

Is returning more than one result - which you are trying to fit it into a single row.

吹泡泡o 2024-09-20 03:13:28

因为你有两行有同一只猫(我认为这意味着类别)

because you have two rows with the same cat (I assume it means category)

辞取 2024-09-20 03:13:28

您有更多该类别 ID 的主题。

You have more topics of that category ID.

过潦 2024-09-20 03:13:28

嗯,是的。它会在您的情况下返回多行。
你想要什么?

如果在子查询末尾添加 LIMIT 1,则可以获得 1 行。
如果您希望每个附加主题都附加一行,则可以使用 JOIN。

well, yes. It returns multiple rows in your case.
What do you want to have?

You can get 1 row if you add LIMIT 1 at the end of the subquery.
You can use JOIN if you want one additional row for each additional topic.

回忆凄美了谁 2024-09-20 03:13:28

因为您使用子查询作为列,所以它必须返回一行。您可以添加 LIMIT 0,1

Because you are using a subquery as a column, so it must return a single row. You could add LIMIT 0,1

你怎么敢 2024-09-20 03:13:28

因为在您的子查询中,您会返回多行主题

并且在选择部分子查询中不允许这种情况

如果您希望类别的用户有多个主题行,请使用JOINS

Because in your sub query you get multiple rows of topic returned.

And in select part sub query such case is not allowed.

Use JOINS if you expect multiple topic rows for user for category.

一瞬间的火花 2024-09-20 03:13:28

你需要加入 2 个表

SELECT name.name, name.cat_id, category.topic
FROM name, category
WHERE name.cat_id = category.cat

you need to join the 2 tables

SELECT name.name, name.cat_id, category.topic
FROM name, category
WHERE name.cat_id = category.cat
樱花坊 2024-09-20 03:13:28

如果您的嵌套选择查询返回多个结果,您可以通过说 SELECT TOP 1 topic from Category... 来仅获取第一个结果...

If your nested select query returns more than one, you can fetch only the first result by saying SELECT TOP 1 topic from category...

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