<?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>in句 &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/in%E5%8F%A5/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Mon, 02 Oct 2023 13:38:36 +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>in句 &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【SQL基礎】in句を使用して複数条件を綺麗に書く｜orで失敗する人へ</title>
		<link>https://sugublog.org/sql-base-in/</link>
					<comments>https://sugublog.org/sql-base-in/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sat, 30 Sep 2023 18:12:37 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[in句]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5595</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/06/pixta_75493348_M-min-e1623599810234.jpg" class="webfeedsFeaturedVisual" /></p>SQLでin句を使用して条件を絞る事が可能です。 SQLで複数の条件を絞りたい時に「and」や「or」を使用すると思います。 しかし「or」を使用すると失敗したり、条件の記述が長くなってしまって綺麗ではない。と言った悩み [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/06/pixta_75493348_M-min-e1623599810234.jpg" class="webfeedsFeaturedVisual" /></p>
<p><strong><span class="swl-inline-color has-swl-deep-01-color">SQLでin句</span></strong>を使用して条件を絞る事が可能です。</p>



<p>SQLで複数の条件を絞りたい時に「and」や「or」を使用すると思います。</p>



<p class="is-style-icon_bad">しかし「or」を使用すると失敗したり、<span class="swl-marker mark_yellow">条件の記述が長くなってしまって綺麗ではない</span>。と言った悩みに当たった事もあると思います。</p>



<p>そこで今回は<strong>プログラミングを普段使用している筆者</strong>が、<strong><span class="swl-marker mark_orange">SQLで条件を絞る時にオススメの「in句」</span></strong>をご紹介します。</p>


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


<p class="has-border -border02"><span class="swl-marker mark_blue"><strong>SQL</strong>で条件を絞りたいけど<strong>「or句」で失敗</strong>してしまう</span>、または<span class="swl-marker mark_orange">もっと条件を<strong>スマートに表示</strong>したい</span>という方は「in句」を使用すれば解決しますよ。</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で「in句」を使用して複数条件を綺麗に書く</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="944" src="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-1024x944.jpg" alt="SQLで「in句」を使用して複数条件を綺麗に書く" class="wp-image-4804" srcset="https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-1024x944.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-300x277.jpg 300w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1-768x708.jpg 768w, https://sugublog.org/wp-content/uploads/2021/12/PC教室20211216-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">SQLで「in句」を使用する基礎構文</h3>



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



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where カラム名 in (条件1,条件2,条件3,・・・);</code></pre></div>



<h3 class="wp-block-heading">SQLで「in句」を使用するメリット</h3>



<p>SQLで複数の条件を「in句」で使用すれば、<span class="swl-bg-color has-swl-deep-02-background-color">「カラム名」を一回しか記述しない</span>のでSQL構文が綺麗になります。</p>



<p>またSQLでorを使用して思い通りにいかない場合も、<strong><span class="swl-bg-color has-swl-deep-01-background-color">「in句」を使用すれば上手くSQLが動く</span></strong>こともあります。</p>



<h2 class="wp-block-heading">SQLで「in句」を実際使用する</h2>



<h3 class="wp-block-heading">SQLで「in句」を実践する</h3>



<p>では<strong><span class="swl-marker mark_orange">実際にSQLで「in句」を使用</span></strong>してみましょう！</p>



<p>下のようなテーブルがあったとします。</p>



<figure class="wp-block-image size-full is-style-border"><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">データベースのテーブル例</figcaption></figure>



<p>年齢が23歳、55歳、37歳の氏名と名前を取り出してみましょう。</p>



<p>「in句」を用いたSQLは以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select `氏名`,`名前`,`年齢` from `人員` where `年齢` in (23,55,37);</code></pre></div>



<p>このSQLを実行してみます。<span class="swl-bg-color has-swl-deep-02-background-color">実行結果</span>は以下となりました。</p>



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



<p>思い通りの結果になっています。</p>



<h3 class="wp-block-heading">「in句」と「or句」を比較してみる</h3>



<p>ではこの<strong><span class="swl-bg-color has-swl-deep-01-background-color">SQL構文を「or句」</span></strong>で表すにはどうすれば良いでしょうか？</p>



<p>先程の結果となるSQL構文は以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select `氏名`,`名前`,`年齢` from `人員` where `年齢` = 23 or `年齢` = 55 or `年齢` = 37;</code></pre></div>



<p>条件３つでも<strong><span class="swl-marker mark_yellow">かなり長いSQL構文</span></strong>になってますね。また<span class="swl-marker mark_yellow">条件の「年齢」を3回も記述</span>しています。</p>



<p><strong>SQLのin句と比べてor句では</strong>条件が増える度に条件が長くなり、条件分「年齢」を記述しないといけません。</p>



<p class="has-border -border02">見にくいですし記述するのも面倒ですので、SQLで複数条件を使用したい場合は「in句」を使用する方がメリットがあります。</p>



<h2 class="wp-block-heading">SQLで「in句」の条件の上限について</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="509" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-5-1024x509.jpg" alt="SQLで「in句」の条件の上限" class="wp-image-4091" srcset="https://sugublog.org/wp-content/uploads/2021/08/school_08-5-1024x509.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/school_08-5-300x149.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/school_08-5-768x382.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/school_08-5.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">SQLで「in句」を使用する時の上限をまとめる</h3>



<p><strong><span class="swl-marker mark_orange">SQLで「in句」を使用する時の上限</span></strong>はあるのでしょうか。</p>



<figure class="wp-block-table td_to_th_"><table class="has-fixed-layout"><thead><tr><th>データベースの種類</th><th>SQLで「in句」の上限数</th></tr></thead><tbody><tr><td>mySQL</td><td>上限サイズまで</td></tr><tr><td>PostgreSQL</td><td>2の64乗、一つの条件が2kバイトまで</td></tr><tr><td>Oracle</td><td>1000個</td></tr></tbody></table><figcaption class="wp-element-caption">SQLのin句での上限まとめ表</figcaption></figure>



<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>



<h3 class="wp-block-heading">mySQLで「in句」の上限を調べるコマンド</h3>



<p>MySQLでの上限サイズは以下のSQLコマンドで確認する事が出来ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>show variables like &#39;max_allowed_packet&#39;;</code></pre></div>



<p>MySQLでの上限サイズを確認する為に、上記のコマンドを実行します。</p>



<figure class="wp-block-table td_to_th_"><table class="has-fixed-layout"><tbody><tr><td>Variable_name</td><td>Value</td></tr><tr><td>max_allowed_packet</td><td>1048576</td></tr></tbody></table><figcaption class="wp-element-caption">mySQLのin句での上限</figcaption></figure>



<p>MySQLでの上限サイズは「1048576バイト」になります。</p>



<h2 class="wp-block-heading">SQLで「in句」の否定系を書く</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/07/pixta_77773496_M-1024x791.jpg" alt="SQLで「in句」の否定系" class="wp-image-3635"/><figcaption class="wp-element-caption">Business person stand on road fork with hard and easy directions. Choice way isometric</figcaption></figure>
</div>


<h3 class="wp-block-heading">SQLの「in句」での否定形構文</h3>



<p>では<strong><span class="swl-bg-color has-swl-deep-02-background-color">SQLの「in句」で否定形</span></strong>を書くにはどうすればよいでしょうか？</p>



<p>答えは以下の構文になります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where カラム名 not in (条件1,条件2,条件3,・・・);</code></pre></div>



<p>「in」の前に「not」を記述するだけです。</p>



<h3 class="wp-block-heading">SQLの「in句」否定形を実行してみる</h3>



<p>では<span class="swl-bg-color has-swl-deep-02-background-color">実際に否定形を実行</span>してみましょう！先程のDBに対して以下のSQLコマンドを実行します！</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select `氏名`,`名前`,`年齢` from `人員` where `年齢` not in (23,55,37);</code></pre></div>



<p>結果は以下となりました。</p>



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



<p>先程のテーブルで「in句」を用いて出した人物以外の人が抽出されているので成功です。</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で「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で「in句」を使用して複数条件を綺麗に書くまとめ" class="wp-image-2884"/></figure>
</div>


<p>今回のまとめです。</p>



<h3 class="wp-block-heading">SQLで「in句」を使用する基礎構文</h3>



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



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where カラム名 in (条件1,条件2,条件3,・・・);</code></pre></div>



<h3 class="wp-block-heading">SQLの「in句」での否定形構文</h3>



<p><strong><span class="swl-bg-color has-swl-deep-02-background-color">SQLの「in句」否定形の基礎構文</span></strong>は以下となります。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-sql" data-lang="SQL"><code>select * from テーブル名 where カラム名 not in (条件1,条件2,条件3,・・・);</code></pre></div>



<h3 class="wp-block-heading">SQLで「in句」を使用する時の上限をまとめる</h3>



<p>SQLで「in句」を使用する時の<span class="swl-marker mark_orange">上限</span>はあるのでしょうか。</p>



<figure class="wp-block-table td_to_th_"><table class="has-fixed-layout"><thead><tr><th>データベースの種類</th><th>SQLで「in句」の上限数</th></tr></thead><tbody><tr><td>mySQL</td><td>上限サイズまで</td></tr><tr><td>PostgreSQL</td><td>2の64乗、一つの条件が2kバイトまで</td></tr><tr><td>Oracle</td><td>1000個</td></tr></tbody></table><figcaption class="wp-element-caption">SQLのin句での上限まとめ表</figcaption></figure>



<p><strong><span class="swl-marker mark_orange">SQLでin句を使用すればorより完結に書ける</span></strong>ことが分かったと思います。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/sql-base-in/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
