MySql 从同一个表中检索数据
我有一个表,其中包含顶级菜单的 id、name、parentId 。他们的孩子喜欢:
--------------------------------------
id | name | parentId
--------------------------------------
1 | Color | 0
--------------------------------------
2 | Flower | 0
--------------------------------------
3 | Red | 1
--------------------------------------
4 | pink | 1
--------------------------------------
5 | Rose | 2
--------------------------------------
6 | Lily | 2
--------------------------------------
我想获取这些记录,结果数组必须像
---------------------------------------------------------------
id | Pname | parentId | child | childId
---------------------------------------------------------------
1 | Color | 1 | Red | 3
---------------------------------------------------------------
2 | Color | 1 | Pink | 4
---------------------------------------------------------------
3 | Flower | 2 | Rose | 5
---------------------------------------------------------------
4 | Flower | 2 | Lily | 6
---------------------------------------------------------------
我的查询一样:
SELECT name AS Pname FROM myTbl WHERE id = (SELECT parentId FROM myTbl WHERE parentId = 1 )
但是 mysql 说#1242 - 子查询返回超过 1 行
有人能解决吗? 谢谢..
I have a table which contains id, name, parentId of Top level Menus & their children like:
--------------------------------------
id | name | parentId
--------------------------------------
1 | Color | 0
--------------------------------------
2 | Flower | 0
--------------------------------------
3 | Red | 1
--------------------------------------
4 | pink | 1
--------------------------------------
5 | Rose | 2
--------------------------------------
6 | Lily | 2
--------------------------------------
And I want to fetch these record some thing that the resultant array must be like
---------------------------------------------------------------
id | Pname | parentId | child | childId
---------------------------------------------------------------
1 | Color | 1 | Red | 3
---------------------------------------------------------------
2 | Color | 1 | Pink | 4
---------------------------------------------------------------
3 | Flower | 2 | Rose | 5
---------------------------------------------------------------
4 | Flower | 2 | Lily | 6
---------------------------------------------------------------
my query was like:
SELECT name AS Pname FROM myTbl WHERE id = (SELECT parentId FROM myTbl WHERE parentId = 1 )
but mysql say #1242 - Subquery returns more than 1 row
Could anyone solve it?
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出现问题是因为您的子查询返回不止一行。
解决方案是确保子查询只返回一行,或者确保它返回更多行并不重要
但是这些并不能真正为您提供表建议您想要的结果
或
Problem arises because your subquery is returning more than one row.
Solution is to ensure that the subquery only ever returns one row, or to make sure it doesn't matter if it returns more
But these don't really give you the result that your table suggests you want
OR