<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>masBlog</title><link>https://masblog.netlify.app/</link><description>Recent content on masBlog</description><generator>Hugo -- 0.146.6</generator><language>en-us</language><lastBuildDate>Wed, 21 May 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://masblog.netlify.app/index.xml" rel="self" type="application/rss+xml"/><item><title>MySQLで2つのテーブルのレコードの差分を確認するクエリ</title><link>https://masblog.netlify.app/articles/mysql%E3%81%A72%E3%81%A4%E3%81%AE%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E5%B7%AE%E5%88%86%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B%E3%82%AF%E3%82%A8%E3%83%AA/</link><pubDate>Wed, 21 May 2025 00:00:00 +0000</pubDate><guid>https://masblog.netlify.app/articles/mysql%E3%81%A72%E3%81%A4%E3%81%AE%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E3%81%AE%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AE%E5%B7%AE%E5%88%86%E3%82%92%E7%A2%BA%E8%AA%8D%E3%81%99%E3%82%8B%E3%82%AF%E3%82%A8%E3%83%AA/</guid><description>&lt;h1 id="本記事はこんな人にオススメ">本記事はこんな人にオススメ&lt;/h1>
&lt;p>テーブルの差分取得できん人&lt;br>
差分取れるけどどこで発生してんのか見づらいわ！と感じでいる人&lt;br>
他のDBでは差分抽出のやり方わかるけどMySQLでどうやんの？って人&lt;/p>
&lt;h1 id="経緯">経緯&lt;/h1>
&lt;p>業務でMySQLのテーブル間の差分を抽出する作業があった際の取り組みをまとめる。&lt;/p>
&lt;h1 id="概要">概要&lt;/h1>
&lt;p>MySQLにおける2つのテーブルのレコードの差分を抽出するクエリをまとめる。&lt;br>
実際に例としてテーブルを用意し、どのようにデータが取得されるのかを記載する。&lt;br>
ただ差分を取得するだけでなく、どこでどのような差分が発生しているかまでフォーカスする。&lt;/p>
&lt;h1 id="検証">検証&lt;/h1>
&lt;p>今回は、実際に以下のuser_example1テーブルとuser_example2テーブルを作成し実際の出力内容の方を確認していく。&lt;br>
user_example1テーブル&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>id&lt;/th>
&lt;th>username&lt;/th>
&lt;th>email&lt;/th>
&lt;th>password&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>Sato&lt;/td>
&lt;td>&lt;a href="mailto:sato@example.com">sato@example.com&lt;/a>&lt;/td>
&lt;td>sato123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>Suzuki&lt;/td>
&lt;td>&lt;a href="mailto:suzuki@example.com">suzuki@example.com&lt;/a>&lt;/td>
&lt;td>suzuki123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>Takahashi&lt;/td>
&lt;td>&lt;a href="mailto:takahashi@example.com">takahashi@example.com&lt;/a>&lt;/td>
&lt;td>takahashi123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>4&lt;/td>
&lt;td>Tanaka&lt;/td>
&lt;td>&lt;a href="mailto:tanaka@example.com">tanaka@example.com&lt;/a>&lt;/td>
&lt;td>tanaka123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>5&lt;/td>
&lt;td>Ito&lt;/td>
&lt;td>&lt;a href="mailto:ito@example.com">ito@example.com&lt;/a>&lt;/td>
&lt;td>ito123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>user_example2テーブル&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>id&lt;/th>
&lt;th>username&lt;/th>
&lt;th>email&lt;/th>
&lt;th>password&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>Sato&lt;/td>
&lt;td>&lt;a href="mailto:sato@example.com">sato@example.com&lt;/a>&lt;/td>
&lt;td>sato123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>Suzuki&lt;/td>
&lt;td>&lt;a href="mailto:suzuki@example.com">suzuki@example.com&lt;/a>&lt;/td>
&lt;td>suzuki123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>Takahashi&lt;/td>
&lt;td>&lt;a href="mailto:takahashi@example.com">takahashi@example.com&lt;/a>&lt;/td>
&lt;td>takahashi123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>4&lt;/td>
&lt;td>Takahashi&lt;/td>
&lt;td>&lt;a href="mailto:tanaka@example.com">tanaka@example.com&lt;/a>&lt;/td>
&lt;td>tanaka123&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>5&lt;/td>
&lt;td>NULL&lt;/td>
&lt;td>&lt;a href="mailto:ito@example.com">ito@example.com&lt;/a>&lt;/td>
&lt;td>ito123&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>差分：&lt;br>
id=4のusernameが異なる&lt;br>
id=5のusernameがNULLになっている&lt;/p>
&lt;h3 id="1-mysql-8031以降の場合">1. MySQL 8.0.31以降の場合&lt;/h3>
&lt;p>EXCEPT演算子を使用することで差分を簡単に取得できる&lt;/p></description></item></channel></rss>