<?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>ハセテツラボ &#187; SQL</title>
	<atom:link href="http://tt-house.com/tag/sql/feed" rel="self" type="application/rss+xml" />
	<link>http://tt-house.com</link>
	<description>生涯一開発屋</description>
	<lastBuildDate>Fri, 03 Sep 2010 14:01:41 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>RubyonRailsでLike演算子を使ったあいまい検索をする方法</title>
		<link>http://tt-house.com/2009/07/rubyonrails-like-query.html</link>
		<comments>http://tt-house.com/2009/07/rubyonrails-like-query.html#comments</comments>
		<pubDate>Tue, 07 Jul 2009 10:38:22 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=104</guid>
		<description><![CDATA[:conditionsにクエリ文字列ごりごり書くなら別に気にしなくてもいいのです...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2009%252F07%252Frubyonrails-like-query.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22RubyonRails%E3%81%A7Like%E6%BC%94%E7%AE%97%E5%AD%90%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E3%81%82%E3%81%84%E3%81%BE%E3%81%84%E6%A4%9C%E7%B4%A2%E3%82%92%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95%22%20%7D);"></div>
<p>:conditionsにクエリ文字列ごりごり書くなら別に気にしなくてもいいのですが、まぁたいていはインジェクション対策も兼ねてプレースホルダーを使うか、シンボルを使うかのどちらかでは無いでしょうか。</p>
<p>ハセテツは前者です。PHPでもPEARを使っていたので、プレースホルダーを使っていました。</p>
<p>で、Rails（ActiveRecord）でプレースホルダーを利用している状況でLike演算子を使ったあいまい検索はどうしたらよいものか、と意外にシンプルな部分で躓いたりします。キーワード検索機能を実装しない限り、Like演算子って使わないですよね。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>:conditions =&gt; ["hoge like ?", "%#{hogeParam}%"]</p></blockquote>
<p>上記の書き方であいまい検索ができました。手抜きではあるのですが、これでhogeParamが空白文字列であっても検索はできます。nilだったらどうしようもないですけどね。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2009/07/rubyonrails-like-query.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLで複数のテーブルを外部結合</title>
		<link>http://tt-house.com/2008/11/mysql-join-table.html</link>
		<comments>http://tt-house.com/2008/11/mysql-join-table.html#comments</comments>
		<pubDate>Thu, 20 Nov 2008 13:42:39 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=74</guid>
		<description><![CDATA[ネストしてもいいのですが、可読性が悪かったりパフォーマンスが悪くなったり。 se...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F11%252Fmysql-join-table.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MySQL%E3%81%A7%E8%A4%87%E6%95%B0%E3%81%AE%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E3%82%92%E5%A4%96%E9%83%A8%E7%B5%90%E5%90%88%22%20%7D);"></div>
<p>ネストしてもいいのですが、可読性が悪かったりパフォーマンスが悪くなったり。</p>
<blockquote style="margin-right: 0px;" dir="ltr"><p>select * from table_0 i<br />
left join table_1 j on i.id = j.id<br />
left join table_2 k on i.id = k.id</p></blockquote>
<p>つまり、「table_0に対してtable_1とtable_2を結合しまっせ。」ということです。ネストしてないのでとっても読みやすいです。パフォーマンスも、よい気がします（計測しろよというご指摘はごもっともです）。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/11/mysql-join-table.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLで先月末日の日付を取得する</title>
		<link>http://tt-house.com/2008/10/mysql-last-date.html</link>
		<comments>http://tt-house.com/2008/10/mysql-last-date.html#comments</comments>
		<pubDate>Tue, 28 Oct 2008 17:26:23 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=70</guid>
		<description><![CDATA[select last_day(date_add(now(), interva...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F10%252Fmysql-last-date.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MySQL%E3%81%A7%E5%85%88%E6%9C%88%E6%9C%AB%E6%97%A5%E3%81%AE%E6%97%A5%E4%BB%98%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%22%20%7D);"></div>
<blockquote style="margin-right: 0px;" dir="ltr"><p>select last_day(date_add(now(), interval -1 month))</p></blockquote>
<p>まぁなんてことない手法なのですが、なんとなく便利だったので記録。「今日の一ヶ月前の月の末日」っていう意味ですね。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/10/mysql-last-date.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLで前月、来月、三日前等の日付を求める</title>
		<link>http://tt-house.com/2008/05/mysql-1.html</link>
		<comments>http://tt-house.com/2008/05/mysql-1.html#comments</comments>
		<pubDate>Mon, 05 May 2008 16:43:22 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[日付関数]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=56</guid>
		<description><![CDATA[select adddate(now(), interval- 3 month...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F05%252Fmysql-1.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MySQL%E3%81%A7%E5%89%8D%E6%9C%88%E3%80%81%E6%9D%A5%E6%9C%88%E3%80%81%E4%B8%89%E6%97%A5%E5%89%8D%E7%AD%89%E3%81%AE%E6%97%A5%E4%BB%98%E3%82%92%E6%B1%82%E3%82%81%E3%82%8B%22%20%7D);"></div>
<blockquote style="margin-right: 0px;" dir="ltr"><p>select adddate(now(), interval- 3 month);</p></blockquote>
<p>これで、3ヶ月前の日付が得られる。引数は前から<br />
<em>「基準日、差分、単位」<br />
</em>となっている。</p>
<p>year,day,hour等も利用可能。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/05/mysql-1.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLでユーザを追加する</title>
		<link>http://tt-house.com/2008/05/mysql.html</link>
		<comments>http://tt-house.com/2008/05/mysql.html#comments</comments>
		<pubDate>Mon, 05 May 2008 11:49:40 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=55</guid>
		<description><![CDATA[ずっとMS SQLSERVERかPostgreSQLをメインに使ってたんだけど、...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F05%252Fmysql.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MySQL%E3%81%A7%E3%83%A6%E3%83%BC%E3%82%B6%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>ずっとMS SQLSERVERかPostgreSQLをメインに使ってたんだけど、最近MySQLに触れることがどんどん増えてきました。まぁ覚えておいて損はないので備忘録に追加。</p>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p>grant all on DB名.* to <a href="mailto:'@'%'">&#8216;ユーザ名&#8217;@'%&#8217;</a> identified by &#8216;パスワード&#8217;;</p>
<p>flush privileges;</p>
</blockquote>
<p>allは全部できまっせ、という意味。DB名のあとのアスタリスクはワイルドカード。すべてのテーブルに対して、という意味。ユーザ名のあとの「%」はどっからでも、という意味。これを「localhost」としておけば、localhostからの接続しか許可しないようになる。まぁMySQLよりも先にLinuxのFWがあるが。</p>
<p>「flush privileges;」は設定情報を反映させるコマンド。ついついコレを忘れてしまう。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/05/mysql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL内でCASEを記述する、条件分岐する</title>
		<link>http://tt-house.com/2008/01/sqlcase.html</link>
		<comments>http://tt-house.com/2008/01/sqlcase.html#comments</comments>
		<pubDate>Sat, 05 Jan 2008 08:39:33 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.tt-house.com//wp/?p=48</guid>
		<description><![CDATA[select case when hoge = 1 then '１でした' w...]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Ftt-house.com%252F2008%252F01%252Fsqlcase.html%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22SQL%E5%86%85%E3%81%A7CASE%E3%82%92%E8%A8%98%E8%BF%B0%E3%81%99%E3%82%8B%E3%80%81%E6%9D%A1%E4%BB%B6%E5%88%86%E5%B2%90%E3%81%99%E3%82%8B%22%20%7D);"></div>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<p dir="ltr" style="MARGIN-RIGHT: 0px">select <br />case <br />when hoge = 1 then &#8216;１でした&#8217; <br />when hoge =&nbsp;2 then &#8216;２でした&#8217; <br />end as hogecase from hogetable</p>
</blockquote>
<p dir="ltr" style="MARGIN-RIGHT: 0px">わかりにくい例ですが、case～endがまぁcaseブロック、when以降が条件、then以降が結果ですね。コレをしらないと、ロジック側でループさせながらぐりぐり判定させないといけないのですが、知っていることによって大幅にロジックを減らせます。</p>
<p dir="ltr" style="MARGIN-RIGHT: 0px">SQLServerやPostgreSQL限定ではなく、SQL-92に載っている手法なので、他のDBでも利用可能です。知っておくべき、活用すべきでしょう。</p>

]]></content:encoded>
			<wfw:commentRss>http://tt-house.com/2008/01/sqlcase.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
