<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>not exists &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/not-exists/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Mon, 02 Oct 2023 13:39:37 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sugublog.org/wp-content/uploads/2021/07/icon_20210703-min-150x150.png</url>
	<title>not exists &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【SQL基礎】exists句とnot exists句の使い方</title>
		<link>https://sugublog.org/sql-base-exists-and-not-exists/</link>
					<comments>https://sugublog.org/sql-base-exists-and-not-exists/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sat, 30 Sep 2023 22:21:01 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[exists]]></category>
		<category><![CDATA[not exists]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5605</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/pixta_77773496_M-e1626536234619-1024x791.jpg" class="webfeedsFeaturedVisual" /></p>SQLで「exists」が出てきた事はありませんか？出てきてその動きが分かりにくく困った事はないでしょうか？ SQLでの「exists」は少し他のコマンドとは違いますのでここにまとめておきます。 exists句は奥が深い [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/07/pixta_77773496_M-e1626536234619-1024x791.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">SQLで「exists」</span></strong>が出てきた事はありませんか？出てきてその動きが分かりにくく困った事はないでしょうか？</p>



<p>SQLでの「exists」は少し他のコマンドとは違いますのでここにまとめておきます。</p>



<p>exists句は奥が深いので今回は基礎の部分だけにしておきます。</p>


<div class="p-blogParts post_content" data-partsID="5030">
<p>windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。</p>
</div>


<p>実際に<span class="swl-marker mark_orange"><strong>「exists句」や否定形の「not exists句」を使用して</strong>動きも見ていくのでわかりやすく</span>なっています。</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">SQLで「exists」と「not exists」の使い方</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="444" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-7-1024x444.jpg" alt="データベース" class="wp-image-4503" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-7-1024x444.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-7-300x130.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-7-768x333.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-7.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">SQLで「exists」と「not exists」の基礎構文</h3>



<p><span class="swl-bg-color has-swl-deep-01-background-color">SQLで「exists句」を使用する時の基礎構文</span>は以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where exists (サブクエリ); </code></pre></div>



<p>SQLで「not exists句」を使用する時の基礎構文はexistsの前に「not」を入れるだけです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where not exists (サブクエリ); </code></pre></div>



<p class="is-style-icon_book">サブクエリとは簡単に言うとSQLの中でSQL構文を使用する事。select構文の中にselect構文が入っているSQLを見た事があると思いますが、selectの中のselectがサブクエリになります。</p>



<h3 class="wp-block-heading">SQLでの「exists」と「not exists」の説明</h3>



<p>existsの特徴１は<span class="swl-bg-color has-swl-deep-02-background-color">引数が１つ</span>でサブクエリになるという事です。</p>



<p>existsの特徴２はwhereの<span class="swl-bg-color has-swl-deep-02-background-color">カラム（existsの左側）を指定しない</span>事です。</p>



<p>カラムを指定しない理由はサブクエリで求めたレコードがある（TRUE）かない（FALSE）かの2択だからです。だからカラムを指定しない</p>



<p>では実際にSQLで「exists」と「not exists」を使用してみましょう！</p>



<h2 class="wp-block-heading">SQLで「exists」と「not exists」を使ってみる</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="682" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg" alt="データベース" class="wp-image-4502" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-1024x682.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5-768x511.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1114-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">SQL実施環境解説</h3>



<p>実施する際の環境を説明します。</p>



<p>以下の2つのテーブルがあるとします。</p>



<div class="swell-block-columns"><div class="swell-block-columns__inner">
<div class="swell-block-column swl-has-mb--s">
<figure class="wp-block-image size-full"><img decoding="async" width="310" height="318" src="https://sugublog.org/wp-content/uploads/2023/10/sql_base_table.png" alt="SQLのテーブル" class="wp-image-5597" srcset="https://sugublog.org/wp-content/uploads/2023/10/sql_base_table.png 310w, https://sugublog.org/wp-content/uploads/2023/10/sql_base_table-292x300.png 292w" sizes="(max-width: 310px) 100vw, 310px" /><figcaption class="wp-element-caption">SQLのテーブル_人員</figcaption></figure>
</div>



<div class="swell-block-column swl-has-mb--s">
<p></p>



<figure class="wp-block-image size-full"><img decoding="async" width="173" height="301" src="https://sugublog.org/wp-content/uploads/2023/10/sql_base_table_tensuhyou.png" alt="SQLのテーブル" class="wp-image-5610"/><figcaption class="wp-element-caption">SQLのテーブル_点数表</figcaption></figure>
</div>
</div></div>



<p>テニスを行った人の氏名、名前、年齢を表示してみます。</p>



<h3 class="wp-block-heading">SQLで「exists」を使ってみる</h3>



<p>以下のSQLコマンドを実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT
    `氏名`,
    `名前`,
    `年齢`
FROM
    `人員` j
WHERE
    EXISTS(
    SELECT
        1
    FROM
        `点数表` t
    WHERE
        j.`番号` = t.`番号` AND t.`競技id` = 3
);</code></pre></div>



<h3 class="wp-block-heading">SQL文の説明</h3>



<p>SELECTで表示するものを記述しています。</p>



<p>FROMで表示するテーブルを記述しています。</p>



<p><strong><span class="swl-marker mark_orange">WHEREで「EXISTS」を指定</span></strong>しています。その後にサブクエリをいれています。</p>



<p>サブクエリのSELECTには「1」を記述していますが「*」でも良いです。</p>



<p class="is-style-icon_pen">「1」に特に意味はありません</p>



<p>サブクエリのFROMでサブクエリするテーブルを記述しています。</p>



<p>サブクエリのWHEREで条件を記述しています。</p>



<p>サブクエリでは<span class="swl-bg-color has-swl-deep-01-background-color">相関サブクエリ</span>をいれますので、<span class="swl-marker mark_orange">表示するテーブルの「番号」と検索するテーブルの「番号」をイコールにしてテーブルをつなげています。</span></p>



<p>ANDで繋げて条件の競技名テニスに対応している「競技id」が3のものを指定しています。</p>



<p class="has-border -border02">これで「競技id」が3になっている番号のレコードが真（TRUE）になり表示されます。</p>



<h3 class="wp-block-heading">SQLで「exists」を実施した結果</h3>



<p>では先程のSQL文を実施してみます。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="125" height="122" src="https://sugublog.org/wp-content/uploads/2023/10/sql_exists.png" alt="SQLでexists句を使用した結果" class="wp-image-5612"/><figcaption class="wp-element-caption">SQLでexists句を使用した結果</figcaption></figure>
</div>


<p>テニスをしている4人が表示されました。</p>



<h3 class="wp-block-heading">SQLで「not exists」を使ってみる</h3>



<p>では今度は<strong><span class="swl-bg-color has-swl-deep-02-background-color">「not exists」を実施</span></strong>します。先程のSQL文の「exists」を「not exists」に変更したものが以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT
    `氏名`,
    `名前`,
    `年齢`
FROM
    `人員` j
WHERE NOT
    EXISTS(
    SELECT
        1
    FROM
        `点数表` t
    WHERE
        j.`番号` = t.`番号` AND t.`競技id` = 3
);</code></pre></div>



<p>SQL構文の説明は「exists」と同じなので省略します。</p>



<h3 class="wp-block-heading">SQLで「not exists」を実施した結果</h3>



<p>では先程のSQL文を実行します。</p>


<div class="wp-block-image is-style-desktop">
<figure class="aligncenter size-full"><img decoding="async" width="120" height="217" src="https://sugublog.org/wp-content/uploads/2023/10/sql_not_exists.png" alt="SQLでnot_exists句を使用した結果" class="wp-image-5613"/><figcaption class="wp-element-caption">SQLでnot_exists句を使用した結果</figcaption></figure>
</div>


<p>テニスをしている人以外が表示されました。</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">SQLで「exists」は「in」で代用可能</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/06/pixta_75493348_M-min-1024x682.jpg" alt="SQLで「exists」は「in」で代用可能" class="wp-image-2884"/></figure>
</div>


<p>実は<span class="swl-marker mark_orange"><strong>「exists句」と「not exists句」</strong>は「in句」でも対応可能</span>です。</p>



<p class="is-style-icon_announce">SQL文でin句を使用する基本は、以下の記事を参照してください。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -external" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">スグブログ</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/06/pixta_75493348_M-min-e1623599810234.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/sql-base-in/" target="_blank" rel="noopener noreferrer">【SQL基礎】in句を使用して複数条件を綺麗に書く｜orで失敗する人へ | in句の上限などもご紹介！</a>
						<span class="p-blogCard__excerpt">SQLでin句を使用して条件を絞る事が可能です。 SQLで複数の条件を絞りたい時に「and」や「or」を使用すると思います。 しかし「or」を使用すると失敗したり、条件の記述が&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>「in句」を使用して同じことを行ってみます。以下のSQL構文を見て下さい。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT
    `氏名`,
    `名前`,
    `年齢`
FROM
    `人員` j
WHERE
    `番号` IN(
    SELECT
        `番号`
    FROM
        `点数表` t
    WHERE
        t.`競技id` = 3
);</code></pre></div>



<p><strong><span class="swl-marker mark_orange">「exists」を使用したSQL文からの変更点</span></strong>としては以下があります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>existsからinにする時の変更点</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>「exists」を「in」に変更する</li>



<li>「in」の前にカラムを記入する</li>



<li>WHERE以降は競技がテニスの「競技id」が3のものを指定</li>
</ul>
</div></div>



<p>ではexistsからinに変更したSQL文を実行してみます。</p>



<figure class="wp-block-image size-full is-style-desktop"><img decoding="async" width="125" height="122" src="https://sugublog.org/wp-content/uploads/2023/10/sql_exists.png" alt="SQLでexists句を使用した結果" class="wp-image-5612"/><figcaption class="wp-element-caption">SQLでin句を使用した結果</figcaption></figure>



<p>「exists」の時と同じ結果になりました。</p>



<p>では<strong><span class="swl-marker mark_orange">「not exists」も「not in」に変更</span></strong>してみます。変更したSQL文は以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT
    `氏名`,
    `名前`,
    `年齢`
FROM
    `人員` j
WHERE
    `番号` NOT IN(
    SELECT
        `番号`
    FROM
        `点数表` t
    WHERE
        t.`競技id` = 3
);</code></pre></div>



<p>ではこのSQL文を実行してみます。</p>



<figure class="wp-block-image size-full is-style-desktop"><img decoding="async" width="120" height="217" src="https://sugublog.org/wp-content/uploads/2023/10/sql_not_exists.png" alt="SQLでnot_exists句を使用した結果" class="wp-image-5613"/><figcaption class="wp-element-caption">SQLでnot_in句を使用した結果</figcaption></figure>



<p>こちらも「not exists」と同じ結果となりました。</p>



<h2 class="wp-block-heading">SQLでexistsとinではどちらを使う</h2>



<p class="is-style-big_icon_hatena">では<strong>SQLでは「exists」と「in」</strong>どちらを使用した方が良いのでしょうか？</p>



<p><span class="swl-marker mark_orange">回答は<strong>MySQLではexists</strong>を使う、<strong>oracleでは同じだけどexists</strong>使う</span></p>



<p>理由は処理速度が違うからです。「exists」MySQLの場合は処理がはやい</p>



<p>oracleは処理速度は同じと書いているのでどちらでも良いですが、MySQLと合わせてexistsを使用するにしました。</p>



<p>なので<strong>「exists」と「in」では</strong><span class="swl-bg-color has-swl-deep-01-background-color"><strong>「exists」</strong>を使用した方が良い</span>となりました。</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">SQLで「exists」と「not exists」の使い方のまとめ</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="560" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg" alt="SQLで「exists」と「not exists」の使い方のまとめ" class="wp-image-4561" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-300x164.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-768x420.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">SQLで「exists」と「not exists」の基礎構文</h3>



<p>SQLで「exists句」を使用する時の基礎構文は以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where exists (サブクエリ); </code></pre></div>



<p>SQLで「not exists句」を使用する時の基礎構文は、existsの前にnotを入れるだけです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where not exists (サブクエリ); </code></pre></div>



<p>「in」でも対応可能ですが「exists」と「in」では「exists」を使用した方が良い。</p>



<p>今回使用したexistsを利用したSQL例文は以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>SELECT
    `氏名`,
    `名前`,
    `年齢`
FROM
    `人員` j
WHERE
    EXISTS(
    SELECT
        1
    FROM
        `点数表` t
    WHERE
        j.`番号` = t.`番号` AND t.`競技id` = 3
);</code></pre></div>



<p>今回はSQLでexistsを使用してみました。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/sql-base-exists-and-not-exists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
