mysql,使用两个select
为什么我收到错误: 子查询返回超过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我可能有一个愚蠢的答案,但你为什么不使用 JOIN ?
I have maybe a silly answer but why arent't you using a JOIN ?
子查询:
返回多个结果 - 您试图将其放入一行。
The sub-query:
Is returning more than one result - which you are trying to fit it into a single row.
因为你有两行有同一只猫(我认为这意味着类别)
because you have two rows with the same cat (I assume it means category)
您有更多该类别 ID 的主题。
You have more topics of that category ID.
嗯,是的。它会在您的情况下返回多行。
你想要什么?
如果在子查询末尾添加 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.
因为您使用子查询作为列,所以它必须返回一行。您可以添加 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
因为在您的子查询中,您会返回多行主题。
并且在选择部分子查询中不允许这种情况。
如果您希望类别的用户有多个主题行,请使用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.
你需要加入 2 个表
you need to join the 2 tables
如果您的嵌套选择查询返回多个结果,您可以通过说 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...