MySql 从同一个表中检索数据

发布于 2024-09-03 22:11:46 字数 1610 浏览 4 评论 0原文

我有一个表,其中包含顶级菜单的 idnameparentId 。他们的孩子喜欢:

--------------------------------------
    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 技术交流群。

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

发布评论

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

评论(2

蓝咒 2024-09-10 22:11:46
SELECT t1.id, t1.name AS Pname, 
    t2.parentId, t2.name as child, t2.id as childId
FROM myTbl t1
INNER JOIN myTbl t2 on t1.id = t2.parentId
SELECT t1.id, t1.name AS Pname, 
    t2.parentId, t2.name as child, t2.id as childId
FROM myTbl t1
INNER JOIN myTbl t2 on t1.id = t2.parentId
以可爱出名 2024-09-10 22:11:46

出现问题是因为您的子查询返回不止一行。
解决方案是确保子查询只返回一行,或者确保它返回更多行并不重要

但是这些并不能真正为您提供表建议您想要的结果

SELECT name AS Pname 
 FROM myTbl 
WHERE id = ( SELECT DISTINCT parentId 
               FROM myTbl 
              WHERE parentId = 1 
           )

SELECT name AS Pname 
 FROM myTbl 
WHERE id IN ( SELECT DISTINCT parentId 
                FROM myTbl 
               WHERE parentId = 1 
            )

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

SELECT name AS Pname 
 FROM myTbl 
WHERE id = ( SELECT DISTINCT parentId 
               FROM myTbl 
              WHERE parentId = 1 
           )

OR

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