久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <tfoot id='bWliB'></tfoot>

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

      • <bdo id='bWliB'></bdo><ul id='bWliB'></ul>

        <i id='bWliB'><tr id='bWliB'><dt id='bWliB'><q id='bWliB'><span id='bWliB'><b id='bWliB'><form id='bWliB'><ins id='bWliB'></ins><ul id='bWliB'></ul><sub id='bWliB'></sub></form><legend id='bWliB'></legend><bdo id='bWliB'><pre id='bWliB'><center id='bWliB'></center></pre></bdo></b><th id='bWliB'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='bWliB'><tfoot id='bWliB'></tfoot><dl id='bWliB'><fieldset id='bWliB'></fieldset></dl></div>
        <legend id='bWliB'><style id='bWliB'><dir id='bWliB'><q id='bWliB'></q></dir></style></legend>

        從 MySQL 中的分層數據生成基于深度的樹(無 CTE)

        Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)(從 MySQL 中的分層數據生成基于深度的樹(無 CTE))
        <legend id='ZFf1R'><style id='ZFf1R'><dir id='ZFf1R'><q id='ZFf1R'></q></dir></style></legend>

        <tfoot id='ZFf1R'></tfoot>

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

                  <tbody id='ZFf1R'></tbody>
                <i id='ZFf1R'><tr id='ZFf1R'><dt id='ZFf1R'><q id='ZFf1R'><span id='ZFf1R'><b id='ZFf1R'><form id='ZFf1R'><ins id='ZFf1R'></ins><ul id='ZFf1R'></ul><sub id='ZFf1R'></sub></form><legend id='ZFf1R'></legend><bdo id='ZFf1R'><pre id='ZFf1R'><center id='ZFf1R'></center></pre></bdo></b><th id='ZFf1R'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ZFf1R'><tfoot id='ZFf1R'></tfoot><dl id='ZFf1R'><fieldset id='ZFf1R'></fieldset></dl></div>
                  <bdo id='ZFf1R'></bdo><ul id='ZFf1R'></ul>
                • 本文介紹了從 MySQL 中的分層數據生成基于深度的樹(無 CTE)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  多年來我一直在 MySQL 中解決這個問題,但是我無法弄清楚.大家有什么建議嗎?

                  Hi For many days I have been working on this problem in MySQL, however I can not figure it out. Do any of you have suggestions?

                  基本上,我有一個類別表,其中包含以下域:id、name(類別名稱)和 parent(父項的 id類別).

                  Basically, I have a category table with domains like: id, name (name of category), and parent (id of parent of the category).

                  示例數據:

                  1  Fruit        0
                  2  Apple        1
                  3  pear         1
                  4  FujiApple    2
                  5  AusApple     2
                  6  SydneyAPPLE  5
                  ....
                  

                  有很多級別,可能超過3個級別.我想創建一個 sql 查詢,根據他的層次結構對數據進行分組:父 > 子 > 孫子 > 等等.

                  There are many levels, possibly more than 3 levels. I want to create an sql query that groups the datas according to he hierarchy: parent > child > grandchild > etc.

                  它應該輸出樹結構,如下:

                  It should output the tree structure, as follows:

                  1 Fruit 0
                   ^ 2 Apple 1
                     ^ 4 FujiApple 2
                     - 5 AusApple 2
                       ^ 6 SydneyApple 5
                   - 3 pear 1
                  

                  我可以使用單個 SQL 查詢來執行此操作嗎?我嘗試過并且確實有效的替代方法如下:

                  Can I do this using a single SQL query? The alternative, which I tried and does work, is the following:

                  SELECT * FROM category WHERE parent=0
                  

                  此后,我再次遍歷數據,并選擇 parent=id 的行.這似乎是一個糟糕的解決方案.因為是mySQL,所以不能使用CTE.

                  After this, I loop through the data again, and select the rows where parent=id. This seems like a bad solution. Because it is mySQL, CTEs cannot be used.

                  推薦答案

                  如果您使用存儲過程,您可以在從 php 到 mysql 的單個調用中完成:

                  You can do it in a single call from php to mysql if you use a stored procedure:

                  mysql> call category_hier(1);
                  
                  +--------+---------------+---------------+----------------------+-------+
                  | cat_id | category_name | parent_cat_id | parent_category_name | depth |
                  +--------+---------------+---------------+----------------------+-------+
                  |      1 | Location      |          NULL | NULL                 |     0 |
                  |      3 | USA           |             1 | Location             |     1 |
                  |      4 | Illinois      |             3 | USA                  |     2 |
                  |      5 | Chicago       |             3 | USA                  |     2 |
                  +--------+---------------+---------------+----------------------+-------+
                  4 rows in set (0.00 sec)
                  
                  
                  $sql = sprintf("call category_hier(%d)", $id);
                  

                  希望這有幫助:)

                  drop table if exists categories;
                  create table categories
                  (
                  cat_id smallint unsigned not null auto_increment primary key,
                  name varchar(255) not null,
                  parent_cat_id smallint unsigned null,
                  key (parent_cat_id)
                  )
                  engine = innodb;
                  

                  測試數據:

                  insert into categories (name, parent_cat_id) values
                  ('Location',null),
                     ('USA',1), 
                        ('Illinois',2), 
                        ('Chicago',2),  
                  ('Color',null), 
                     ('Black',3), 
                     ('Red',3);
                  

                  程序:

                  drop procedure if exists category_hier;
                  
                  delimiter #
                  
                  create procedure category_hier
                  (
                  in p_cat_id smallint unsigned
                  )
                  begin
                  
                  declare v_done tinyint unsigned default 0;
                  declare v_depth smallint unsigned default 0;
                  
                  create temporary table hier(
                   parent_cat_id smallint unsigned, 
                   cat_id smallint unsigned, 
                   depth smallint unsigned default 0
                  )engine = memory;
                  
                  insert into hier select parent_cat_id, cat_id, v_depth from categories where cat_id = p_cat_id;
                  
                  /* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */
                  
                  create temporary table tmp engine=memory select * from hier;
                  
                  while not v_done do
                  
                      if exists( select 1 from categories p inner join hier on p.parent_cat_id = hier.cat_id and hier.depth = v_depth) then
                  
                          insert into hier 
                              select p.parent_cat_id, p.cat_id, v_depth + 1 from categories p 
                              inner join tmp on p.parent_cat_id = tmp.cat_id and tmp.depth = v_depth;
                  
                          set v_depth = v_depth + 1;          
                  
                          truncate table tmp;
                          insert into tmp select * from hier where depth = v_depth;
                  
                      else
                          set v_done = 1;
                      end if;
                  
                  end while;
                  
                  select 
                   p.cat_id,
                   p.name as category_name,
                   b.cat_id as parent_cat_id,
                   b.name as parent_category_name,
                   hier.depth
                  from 
                   hier
                  inner join categories p on hier.cat_id = p.cat_id
                  left outer join categories b on hier.parent_cat_id = b.cat_id
                  order by
                   hier.depth, hier.cat_id;
                  
                  drop temporary table if exists hier;
                  drop temporary table if exists tmp;
                  
                  end #
                  

                  測試運行:

                  delimiter ;
                  
                  call category_hier(1);
                  
                  call category_hier(2);
                  

                  一些使用 Yahoo geoplanet 位置數據的性??能測試

                  drop table if exists geoplanet_places;
                  create table geoplanet_places
                  (
                  woe_id int unsigned not null,
                  iso_code  varchar(3) not null,
                  name varchar(255) not null,
                  lang varchar(8) not null,
                  place_type varchar(32) not null,
                  parent_woe_id int unsigned not null,
                  primary key (woe_id),
                  key (parent_woe_id)
                  )
                  engine=innodb;
                  
                  mysql> select count(*) from geoplanet_places;
                  +----------+
                  | count(*) |
                  +----------+
                  |  5653967 |
                  +----------+
                  

                  表中有 560 萬行(位置),讓我們看看從 php 調用的鄰接列表實現/存儲過程是如何處理的.

                  so that's 5.6 million rows (places) in the table let's see how the adjacency list implementation/stored procedure called from php handles that.

                       1 records fetched with max depth 0 in 0.001921 secs
                     250 records fetched with max depth 1 in 0.004883 secs
                     515 records fetched with max depth 1 in 0.006552 secs
                     822 records fetched with max depth 1 in 0.009568 secs
                     918 records fetched with max depth 1 in 0.009689 secs
                    1346 records fetched with max depth 1 in 0.040453 secs
                    5901 records fetched with max depth 2 in 0.219246 secs
                    6817 records fetched with max depth 1 in 0.152841 secs
                    8621 records fetched with max depth 3 in 0.096665 secs
                   18098 records fetched with max depth 3 in 0.580223 secs
                  238007 records fetched with max depth 4 in 2.003213 secs
                  

                  總的來說,我對那些冷運行時非常滿意,因為我什至不會考慮將數萬行數據返回到我的前端,而是寧愿構建樹,每次調用只獲取幾個級別.哦,以防萬一你認為 innodb 比 myisam 慢——我測試的 myisam 實現在所有方面都慢了兩倍.

                  Overall i'm pretty pleased with those cold runtimes as I wouldn't even begin to consider returning tens of thousands of rows of data to my front end but would rather build the tree dynamically fetching only several levels per call. Oh and just incase you were thinking innodb is slower than myisam - the myisam implementation I tested was twice as slow in all counts.

                  更多內容:http://pastie.org/1672733

                  希望這有幫助:)

                  這篇關于從 MySQL 中的分層數據生成基于深度的樹(無 CTE)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
                  reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                  Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                  Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                  pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                  How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)
                    <tbody id='sZaOR'></tbody>

                  1. <small id='sZaOR'></small><noframes id='sZaOR'>

                  2. <tfoot id='sZaOR'></tfoot>
                        <bdo id='sZaOR'></bdo><ul id='sZaOR'></ul>

                          • <legend id='sZaOR'><style id='sZaOR'><dir id='sZaOR'><q id='sZaOR'></q></dir></style></legend>

                          • <i id='sZaOR'><tr id='sZaOR'><dt id='sZaOR'><q id='sZaOR'><span id='sZaOR'><b id='sZaOR'><form id='sZaOR'><ins id='sZaOR'></ins><ul id='sZaOR'></ul><sub id='sZaOR'></sub></form><legend id='sZaOR'></legend><bdo id='sZaOR'><pre id='sZaOR'><center id='sZaOR'></center></pre></bdo></b><th id='sZaOR'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='sZaOR'><tfoot id='sZaOR'></tfoot><dl id='sZaOR'><fieldset id='sZaOR'></fieldset></dl></div>

                            主站蜘蛛池模板: 久久久久无码国产精品一区 | 99这里只有精品视频 | 久久高清 | 国产精品久久午夜夜伦鲁鲁 | 日韩手机在线视频 | 国产乱码高清区二区三区在线 | 日日摸夜夜添夜夜添特色大片 | 欧美国产中文 | 日韩欧美一区二区三区在线播放 | 国产一区二区久久 | 国产精品久久久久久久久久久久久 | 国产在线不卡 | 97人人草| 在线成人福利 | 中文字幕视频在线看 | 国产激情视频网站 | 日韩欧美在线观看 | 成人免费看电影 | 91精品国产91久久久久久最新 | 黄色毛片网站在线观看 | 成人依人 | 国产亚洲人成a在线v网站 | 91看片视频| 亚洲精品一 | 一区二区av在线 | 久久久精品一区二区三区 | 日韩av电影院 | 日本一区二区不卡 | 超碰高清| av看片| 在线观看国产 | 国产欧美一区二区精品久导航 | 91精品国产综合久久国产大片 | 久久99精品视频 | 亚洲视频免费观看 | 综合精品在线 | 国产一区在线免费观看 | 在线视频一区二区 | 亚洲精品一区二区网址 | 欧美日产国产成人免费图片 | 久久精品视频在线播放 |