Skip to Content
SQLクックブック 第2版 ―データベースエキスパート、データサイエンティストのための実践レシピ集
book

SQLクックブック 第2版 ―データベースエキスパート、データサイエンティストのための実践レシピ集

by Anthony Molinaro, Robert de Graaf, 嶋田 健志, 木下 哲也
February 2022
Intermediate to advanced
560 pages
8h 5m
Japanese
O'Reilly Japan, Inc.
Content preview from SQLクックブック 第2版 ―データベースエキスパート、データサイエンティストのための実践レシピ集

13章階層クエリ

本章では、データ内で生じる可能性のある階層関係を表すためのレシピを紹介します。階層的なデータを扱う際には、データの保存よりも階層としてのデータの取得や表示の方が難しいのが普通です。

MySQLに再帰CTEが追加されてからまだ数年しか経っていませんが、MySQLで利用できるようになったことで、実質的にすべてのRDBMSで再帰CTEが利用できるようになりました。結果として再帰CTEは階層クエリを扱う標準的な手法となりました。本章ではこの機能を自在に利用してデータの階層構造を解明するためのレシピを紹介します。

本題に入る前に、EMPテーブルのEMPNOMGR間の階層関係を調べましょう。

select empno,mgr
  from emp
 order by 2

     EMPNO        MGR
---------- ----------
      7788       7566
      7902       7566
      7499       7698
      7521       7698
      7900       7698
      7844       7698
      7654       7698
      7934       7782
      7876       7788
      7566       7839
      7782       7839
      7698       7839
      7369       7902
      7839

これをよく見ると、MGRの値もEMPNOであることわかります。つまり、EMPテーブルの従業員の管理職もEMPテーブルの従業員であり、別のどこかに格納されているわけではありません。MGREMPNO間の関係は、MGRの値があるEMPNOの直近の親であるという点で親子関係です(また、ある特定の従業員の管理職も管理職を持ち、その管理職がまた管理職を持つといったようにn層の階層を形成する可能性もあります)。従業員が管理職を持たない場合には、MGRNULLになります。

レシピ13.1 親子関係を表す ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

マスタリングAPIアーキテクチャ ―モノリシックからマイクロサービスへとアーキテクチャを進化させるための実践的手法

マスタリングAPIアーキテクチャ ―モノリシックからマイクロサービスへとアーキテクチャを進化させるための実践的手法

James Gough, Daniel Bryant, Matthew Auburn, 石川 朝久
機械学習デザインパターン ―データ準備、モデル構築、MLOpsの実践上の問題と解決

機械学習デザインパターン ―データ準備、モデル構築、MLOpsの実践上の問題と解決

Valliappa Lakshmanan, Sara Robinson, Michael Munn, 鷲崎 弘宜, 竹内 広宜, 名取 直毅, 吉岡 信和

Publisher Resources

ISBN: 9784873119779Other