• <small id='1Tb1G'></small><noframes id='1Tb1G'>

    <tfoot id='1Tb1G'></tfoot>

      <bdo id='1Tb1G'></bdo><ul id='1Tb1G'></ul>

        <i id='1Tb1G'><tr id='1Tb1G'><dt id='1Tb1G'><q id='1Tb1G'><span id='1Tb1G'><b id='1Tb1G'><form id='1Tb1G'><ins id='1Tb1G'></ins><ul id='1Tb1G'></ul><sub id='1Tb1G'></sub></form><legend id='1Tb1G'></legend><bdo id='1Tb1G'><pre id='1Tb1G'><center id='1Tb1G'></center></pre></bdo></b><th id='1Tb1G'></th></span></q></dt></tr></i><div id='1Tb1G'><tfoot id='1Tb1G'></tfoot><dl id='1Tb1G'><fieldset id='1Tb1G'></fieldset></dl></div>

        <legend id='1Tb1G'><style id='1Tb1G'><dir id='1Tb1G'><q id='1Tb1G'></q></dir></style></legend>

      1. 在一个 SQL 查询中获取所有父行

        时间:2023-05-23

            <bdo id='DgiIl'></bdo><ul id='DgiIl'></ul>

            1. <legend id='DgiIl'><style id='DgiIl'><dir id='DgiIl'><q id='DgiIl'></q></dir></style></legend>

              <small id='DgiIl'></small><noframes id='DgiIl'>

              <tfoot id='DgiIl'></tfoot>
                • <i id='DgiIl'><tr id='DgiIl'><dt id='DgiIl'><q id='DgiIl'><span id='DgiIl'><b id='DgiIl'><form id='DgiIl'><ins id='DgiIl'></ins><ul id='DgiIl'></ul><sub id='DgiIl'></sub></form><legend id='DgiIl'></legend><bdo id='DgiIl'><pre id='DgiIl'><center id='DgiIl'></center></pre></bdo></b><th id='DgiIl'></th></span></q></dt></tr></i><div id='DgiIl'><tfoot id='DgiIl'></tfoot><dl id='DgiIl'><fieldset id='DgiIl'></fieldset></dl></div>
                    <tbody id='DgiIl'></tbody>
                  本文介绍了在一个 SQL 查询中获取所有父行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个简单的 MySQL 表,其中包含一个类别列表,级别由 parent_id 决定:

                  I have a simple MySQL table thats contains a list of categories, level is determined by parent_id:

                  id  name    parent_id
                  ---------------------------
                  1   Home        0
                  2   About       1
                  3   Contact     1
                  4   Legal       2
                  5   Privacy     4
                  6   Products    1
                  7   Support     1
                  

                  我正在尝试制作面包屑路径.所以我有孩子的id",我想得到所有可用的父母(迭代链直到我们到达 0家").可以有任意数量或子行进入无限深度.

                  I'm attempting to make a breadcrumb trail. So i have the 'id' of the child, I want to get all available parents (iterating up the chain until we reach 0 "Home"). There could be any number or child rows going to an unlimited depth.

                  目前我正在为每个父级使用 SQL 调用,这很麻烦.SQL 中有没有办法在一个查询中完成所有这些操作?

                  Currently I am using an SQL call for each parent, this is messy. Is there a way in SQL to do this all on one query?

                  推荐答案

                  改编自 这里:

                  SELECT T2.id, T2.name
                  FROM (
                      SELECT
                          @r AS _id,
                          (SELECT @r := parent_id FROM table1 WHERE id = _id) AS parent_id,
                          @l := @l + 1 AS lvl
                      FROM
                          (SELECT @r := 5, @l := 0) vars,
                          table1 h
                      WHERE @r <> 0) T1
                  JOIN table1 T2
                  ON T1._id = T2.id
                  ORDER BY T1.lvl DESC
                  

                  @r := 5 是当前页面的页码.结果如下:

                  The line @r := 5 is the page number for the current page. The result is as follows:

                  1, 'Home'
                  2, 'About'
                  4, 'Legal'
                  5, 'Privacy'
                  

                  这篇关于在一个 SQL 查询中获取所有父行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:实体框架 6 不能使用 MySQL 连接 下一篇:如何选择具有当天时间戳的行?

                  相关文章

                  1. <i id='Q2si1'><tr id='Q2si1'><dt id='Q2si1'><q id='Q2si1'><span id='Q2si1'><b id='Q2si1'><form id='Q2si1'><ins id='Q2si1'></ins><ul id='Q2si1'></ul><sub id='Q2si1'></sub></form><legend id='Q2si1'></legend><bdo id='Q2si1'><pre id='Q2si1'><center id='Q2si1'></center></pre></bdo></b><th id='Q2si1'></th></span></q></dt></tr></i><div id='Q2si1'><tfoot id='Q2si1'></tfoot><dl id='Q2si1'><fieldset id='Q2si1'></fieldset></dl></div>
                    1. <legend id='Q2si1'><style id='Q2si1'><dir id='Q2si1'><q id='Q2si1'></q></dir></style></legend>
                      • <bdo id='Q2si1'></bdo><ul id='Q2si1'></ul>

                      <small id='Q2si1'></small><noframes id='Q2si1'>

                    2. <tfoot id='Q2si1'></tfoot>