SlideShare a Scribd company logo
1 of 13
ここに「ORDER BY 姓、名、ID」の順で表示されるテーブルTがあります。 ID 姓 名 2 あああ うみこ 1 ああい いちろう 3 ああう あきら ここで、ID=1「ああい いちろう」の情報を表示する画面を考えます。 項目名 値 ID 1 姓 ああい 名 いちろう ← 一つ前(あああ うみこ)へ      一つ後(ああう あきら)へ-> このとき、上図の一つ前と一つ後のリンクに使われている 「一つ前のレコードの姓名」と「一つ後ろのレコードの姓名」を 一回で取得するSQL文を考えてください。 なお、以下に注意してください。  ・DBMSはMySQL(OracleでいうROW_NUMBER等は非対応)とします。  ・テーブルTにはID,姓,名以外のフィールドはありません。  ・同姓同名もありうるものとします。  ・IDはソート順を指すものではありません。  ・サブクエリは使用できます。
まずは現在位置を特定 あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
姓名順ではなくID順なら SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
SELECT CONCAT(`姓`,`名`) FROM `test` WHERE ID='1' SELECT CONCAT(`姓`,`名`) FROM `test` WHERE ID  >  '1' ORDER BY `ID` LIMIT 1 ああい いちろう ID:1 あああ うみこ ID:2 ああう あきら ID:3
Oracleなら SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' SELECT CONCAT( LAG(` 姓 `) OVER( ORDER BY ` 姓 `,` 名 `,`ID`), LAG(` 名 `) OVER( ORDER BY ` 姓 `,` 名 `,`ID`) ) FROM `test` WHERE ID = '1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
MySQLでがんばる SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' SELECT (SELECT  CONCAT(` 姓 `,` 名 `)  FROM `test` WHERE (` 姓 ` < `T`.` 姓 `)  --  より若い姓 OR(` 姓 ` = `T`.` 姓 ` AND ` 名 ` < `T`.` 名 `)  --  より若い名 OR(` 姓 ` = `T`.` 姓 ` AND ` 名 ` = `T`.` 名 ` AND `ID` < `T`.`ID`)  --  より若い ID ORDER BY ` 姓 ` DESC, ` 名 ` DESC, `ID` DESC LIMIT 1  --  姓名 ID の降順で最初に採用されるレコード ) AS ` 一つ前のレコードの姓名 ` FROM `test`  AS `T`   WHERE ID = '1' ああい いちろう ID:1 あああ うみこ ID:2 ああう あきら ID:3
SELECT (SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE (` 姓 ` < `T`.` 姓 `)  --  より若い姓 OR (` 姓 ` = `T`.` 姓 ` AND ` 名 ` < `T`.` 名 `)  --  より若い名 OR (` 姓 ` = `T`.` 姓 ` AND ` 名 ` = `T`.` 名 ` AND   `test`.`ID` < `T`.`ID`)  --  より若い ID   ORDER BY ` 姓 ` DESC, ` 名 ` DESC, `ID` DESC LIMIT 1  --  姓名 ID の降順で最初に採用されるレコード ) AS ` 一つ前のレコードの姓名 ` FROM `test`  AS `T`   WHERE ID = '1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
 
 

More Related Content

Recently uploaded

AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfFumieNakayama
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfFumieNakayama
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineerYuki Kikuchi
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...博三 太田
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)Hiroshi Tomioka
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)UEHARA, Tetsutaro
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?akihisamiyanaga1
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案sugiuralab
 

Recently uploaded (8)

AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdfAWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
 
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdfクラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
 
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
 
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察  ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
 
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
業務で生成AIを活用したい人のための生成AI入門講座(社外公開版:キンドリルジャパン社内勉強会:2024年4月発表)
 
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
 
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
 

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Data sorting by SQL

  • 1. ここに「ORDER BY 姓、名、ID」の順で表示されるテーブルTがあります。 ID 姓 名 2 あああ うみこ 1 ああい いちろう 3 ああう あきら ここで、ID=1「ああい いちろう」の情報を表示する画面を考えます。 項目名 値 ID 1 姓 ああい 名 いちろう ← 一つ前(あああ うみこ)へ      一つ後(ああう あきら)へ-> このとき、上図の一つ前と一つ後のリンクに使われている 「一つ前のレコードの姓名」と「一つ後ろのレコードの姓名」を 一回で取得するSQL文を考えてください。 なお、以下に注意してください。  ・DBMSはMySQL(OracleでいうROW_NUMBER等は非対応)とします。  ・テーブルTにはID,姓,名以外のフィールドはありません。  ・同姓同名もありうるものとします。  ・IDはソート順を指すものではありません。  ・サブクエリは使用できます。
  • 2. まずは現在位置を特定 あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 3. SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 4. 姓名順ではなくID順なら SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 5. SELECT CONCAT(`姓`,`名`) FROM `test` WHERE ID='1' SELECT CONCAT(`姓`,`名`) FROM `test` WHERE ID > '1' ORDER BY `ID` LIMIT 1 ああい いちろう ID:1 あああ うみこ ID:2 ああう あきら ID:3
  • 6. Oracleなら SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 7. SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' SELECT CONCAT( LAG(` 姓 `) OVER( ORDER BY ` 姓 `,` 名 `,`ID`), LAG(` 名 `) OVER( ORDER BY ` 姓 `,` 名 `,`ID`) ) FROM `test` WHERE ID = '1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 8. MySQLでがんばる SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 9. SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE ID='1' SELECT (SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE (` 姓 ` < `T`.` 姓 `) -- より若い姓 OR(` 姓 ` = `T`.` 姓 ` AND ` 名 ` < `T`.` 名 `) -- より若い名 OR(` 姓 ` = `T`.` 姓 ` AND ` 名 ` = `T`.` 名 ` AND `ID` < `T`.`ID`) -- より若い ID ORDER BY ` 姓 ` DESC, ` 名 ` DESC, `ID` DESC LIMIT 1 -- 姓名 ID の降順で最初に採用されるレコード ) AS ` 一つ前のレコードの姓名 ` FROM `test` AS `T` WHERE ID = '1' ああい いちろう ID:1 あああ うみこ ID:2 ああう あきら ID:3
  • 10. SELECT (SELECT CONCAT(` 姓 `,` 名 `) FROM `test` WHERE (` 姓 ` < `T`.` 姓 `) -- より若い姓 OR (` 姓 ` = `T`.` 姓 ` AND ` 名 ` < `T`.` 名 `) -- より若い名 OR (` 姓 ` = `T`.` 姓 ` AND ` 名 ` = `T`.` 名 ` AND `test`.`ID` < `T`.`ID`) -- より若い ID ORDER BY ` 姓 ` DESC, ` 名 ` DESC, `ID` DESC LIMIT 1 -- 姓名 ID の降順で最初に採用されるレコード ) AS ` 一つ前のレコードの姓名 ` FROM `test` AS `T` WHERE ID = '1' あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 11. あああ うみこ ID:2 ああう あきら ID:3 ああい いちろう ID:1
  • 12.  
  • 13.