<?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>HattaraTecLog &#187; SQL</title>
	<atom:link href="http://teclog.hattara.info/?cat=30&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://teclog.hattara.info</link>
	<description>LinuxネタとかPHPネタとかの技術ログ</description>
	<lastBuildDate>Tue, 18 Nov 2014 12:15:20 +0000</lastBuildDate>
	<language>ja</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>SQLで○○以外()の時のNULLには気をつけろ！</title>
		<link>http://teclog.hattara.info/?p=264</link>
		<comments>http://teclog.hattara.info/?p=264#comments</comments>
		<pubDate>Mon, 29 Nov 2010 10:48:53 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[未分類]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=264</guid>
		<description><![CDATA[今日実際にこんな現象を見たので、メモ的に残しておきます。 SQLで何かするときに、以下のようなパターンがあると [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>今日実際にこんな現象を見たので、メモ的に残しておきます。<br />
SQLで何かするときに、以下のようなパターンがあると思う。</p>
<blockquote><p>
| id | val1 |<br />
| 1  |  5    |<br />
| 2  |  10  |<br />
| 3  |  1    |<br />
| 4  |  100 |<br />
| 5  |  NULL |<br />
| 6  |  20   |
</p></blockquote>
<p>この場合に、NULLをひっぱったりするときに　IS を利用するのは知ってました。</p>
<blockquote><p>
SELECT id, val1 FROM test WHERE val1 is NULL;
</p></blockquote>
<p>ここで今回のメモは、</p>
<blockquote><p>
SELECT id, val1 FROM test WHERE val1 <> 100;
</p></blockquote>
<p>このようにした場合に、NULLはHITしないということです。<br />
型が違うためかどうかわかりませんが、そういうことです。<br />
対策としては、NULLについても別途条件を追加することです。</p>
<blockquote><p>
SELECT id, val1 FROM test WHERE (val1 is NULL or val1 <> 100);
</p></blockquote>
<p>これで、あなたにも明るい未来が見えることでしょう。</p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=264</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQLで行番号表示</title>
		<link>http://teclog.hattara.info/?p=75</link>
		<comments>http://teclog.hattara.info/?p=75#comments</comments>
		<pubDate>Mon, 06 Apr 2009 14:02:28 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=75</guid>
		<description><![CDATA[MySQLで行番号を表示したかったんですが、どうしたものか調べていたら、 無常迅速さんのサイトで よさげなもの [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>MySQLで行番号を表示したかったんですが、どうしたものか調べていたら、<br />
<a href="http://meg-39love.blogspot.com/2008/06/mysql.html">無常迅速</a>さんのサイトで<br />
よさげなものを見つけました。</p>
<blockquote><p>SET @i:=0;<br />
SELECT<br />
    (@i:=@i+1) as row_number, a_col, b_col, c_col, d_col<br />
FROM<br />
    test_table<br />
ORDER BY<br />
    b_col;</p></blockquote>
<p>みたいな感じにすると、row_numberのカラムに行番号が表示できちゃいました。<br />
これはちょっとありがたい。</p>
<p>レコードで何番目のデータかとか出す時には便利ですね。</p>
<p><a href="http://px.a8.net/svt/ejp?a8mat=U70LG+CVSQKI+5WS+BWVTD&#038;a8ejpredirect=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F%25E8%25A9%25B3%25E8%25A7%25A3MySQL-%25E3%2582%25B5%25E3%2583%25BC%25E3%2582%25B7%25E3%2583%25A3%25E3%2583%25BB%25E3%2583%2591%25E3%2583%2581%25E3%2582%25A7%25E3%2583%2595-9784873113432%2Fitem%2F5136616%2F" target="_blank"></p>
<p style="text-align:center">MySQLのおすすめ書籍</p>
<p><img border="0" alt="" src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311343.jpg?_ex=200x200&#038;s=2&#038;r=1"></a><br />
<img border="0" width="1" height="1" src="http://www14.a8.net/0.gif?a8mat=U70LG+CVSQKI+5WS+BWVTD" alt=""></p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=75</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DB Error: mismatchがでたぞ!!</title>
		<link>http://teclog.hattara.info/?p=73</link>
		<comments>http://teclog.hattara.info/?p=73#comments</comments>
		<pubDate>Thu, 02 Apr 2009 09:17:38 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[エラー]]></category>
		<category><![CDATA[プレースホルダ]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=73</guid>
		<description><![CDATA[php経由でMysqlに接続して処理してたときに なんでか理由がわからないけどエラーがでた。 DB Error [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>php経由でMysqlに接続して処理してたときに<br />
なんでか理由がわからないけどエラーがでた。</p>
<blockquote><p>DB Error: mismatch</p></blockquote>
<p>試した感じからすると、where句の内容に問題があるようだ。</p>
<p>【エラーが出る例】</p>
<blockquote><p>select * from table where column != ? ;</p></blockquote>
<p>　  ※「?」はプレースホルダしてるから。<br />
　　　ちなみにSSH経由でmysqlコマンドで接続して実行するとエラーにならない。</p>
<p>【エラーが出ない例】</p>
<blockquote><p>　select * from table where column <> ? ;</p></blockquote>
<p>　  ※これだとエラーにならないわけです。</p>
<p>なんでですかね？<br />
　phpとの相性？<br />
　プレースホルダ絡み？<br />
　phpの書き方に問題があった？</p>
<p>ちょっとわからないけど、備忘録として残しておきます。<br />
知ってる人いたら、コメントください。</p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=73</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQLで&#8221;The used SELECT statements have a different number of columns&#8221;</title>
		<link>http://teclog.hattara.info/?p=71</link>
		<comments>http://teclog.hattara.info/?p=71#comments</comments>
		<pubDate>Fri, 27 Mar 2009 11:22:34 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=71</guid>
		<description><![CDATA[PHP＆MySQLでSQLを書いてたときに、 &#8220;The used SELECT statement [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP＆MySQLでSQLを書いてたときに、</p>
<blockquote><p>&#8220;The used SELECT statements have a different number of columns&#8221;</p></blockquote>
<p>みたいなエラーがでました。<br />
グーグル先生に聞いたところ、<a href="http://dev.mysql.com/doc/refman/4.1/ja/error-returns.html">MySQL本家サイト</a>にも記述がありました。</p>
<p>理由としては、<br />
UNIONを利用していて、片方だけにしか特定のカラムがない場合にでるようです。<br />
出力カラムの数を数えるのと、カラム名を確認してみたらいいんじゃないでしょうか。</p>
<p>僕は単純にカラム数(column)がたりませんでした。</p>
<p>注意力が足りなくて、スミマメーン！</p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=71</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQLでテーブルをコピー＆リネーム</title>
		<link>http://teclog.hattara.info/?p=51</link>
		<comments>http://teclog.hattara.info/?p=51#comments</comments>
		<pubDate>Thu, 30 Oct 2008 10:02:35 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=51</guid>
		<description><![CDATA[MySQLを利用していて、テーブルをリネームしてみたくなったり、 コピーしてみたくなったりすることがありますよ [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>MySQLを利用していて、テーブルをリネームしてみたくなったり、<br />
コピーしてみたくなったりすることがありますよね。</p>
<p>なので、一応まとめときます。</p>
<p>【テーブルリネーム】</p>
<blockquote><p>
RENAME TABLE before_table_name TO after_table_name;</p>
<p>例：<br />
　　変更前のテーブル名が　「tec」　で<br />
　　変更後のテーブル名を　「log」 にしたい場合には、</p>
<p>    RENAME TABLE tec to log;
</p></blockquote>
<p>次に特定のテーブルをコピーしたい場合です。</p>
<p>【テーブルコピー】</p>
<blockquote><p>
CREATE TABLE new_table AS SELECT * FROM old_table;</p>
<p>例1：<br />
　　コピー元のテーブル名が　「apple」 で<br />
　　コピー先「新規作成」のテーブル名が　「melon」の場合には、</p>
<p>    CREATE TABLE melon AS SELECT * FROM apple;</p>
<p>例2:<br />
    特定の条件のもののみコピーしたい場合には WHERE句で指定して<br />
　　コピーすることもできます。</p>
<p>　　コピー元のテーブル名が　「all」 で<br />
　　コピー先「新規作成」のテーブル名が　「now」で<br />
　　「Year」カラムが「2008」のもののみ抜き出す場合には、</p>
<p>　　CREATE TABLE now AS SELECT * FROM all WHERE Year = &#8217;2008&#8242;;
</p></blockquote>
<p>というような形になりますね。</p>
<p>ということで、久々の更新はこんな感じで。</p>
<p><a href="http://px.a8.net/svt/ejp?a8mat=U70LG+CVSQKI+5WS+BWVTD&#038;a8ejpredirect=http%3A%2F%2Fbooks.rakuten.co.jp%2Frb%2F%25E8%25A9%25B3%25E8%25A7%25A3MySQL-%25E3%2582%25B5%25E3%2583%25BC%25E3%2582%25B7%25E3%2583%25A3%25E3%2583%25BB%25E3%2583%2591%25E3%2583%2581%25E3%2582%25A7%25E3%2583%2595-9784873113432%2Fitem%2F5136616%2F" target="_blank"></p>
<p style="text-align:center">MySQLのおすすめ書籍</p>
<p><img border="0" alt="" src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311343.jpg?_ex=200x200&#038;s=2&#038;r=1"></a><br />
<img border="0" width="1" height="1" src="http://www14.a8.net/0.gif?a8mat=U70LG+CVSQKI+5WS+BWVTD" alt=""></p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=51</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQLの更新系SQLを省略する(autoExecute)</title>
		<link>http://teclog.hattara.info/?p=37</link>
		<comments>http://teclog.hattara.info/?p=37#comments</comments>
		<pubDate>Fri, 10 Oct 2008 09:55:26 +0000</pubDate>
		<dc:creator><![CDATA[hattara]]></dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[更新]]></category>

		<guid isPermaLink="false">http://teclog.hattara.info/?p=37</guid>
		<description><![CDATA[MySQLでinsertとかUpdateとかするときに、 SQLをかかずにサクサクやる方法があるそうな。 その [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>MySQLでinsertとかUpdateとかするときに、<br />
SQLをかかずにサクサクやる方法があるそうな。<br />
その名も「autoExecute」なり。</p>
<p>Insertしたい場合には、以下のような書き方になるようです。</p>
<blockquote><p>
$test1 = &#8220;test&#8221;;<br />
$table = &#8220;mysql_test_table&#8221;</p>
<p>$Connect = DB::connect($Dsn);<br />
if (DB::iserror($Connect)){<br />
    die($Connect->getMessage());<br />
}</p>
<p>$Value = array(<br />
			&#8216;name&#8217;   => &#8216;hattara&#8217;,<br />
			&#8216;domain&#8217; => &#8216;teclog.hattara.info&#8217;,<br />
			&#8216;col1&#8242;   => $test1,<br />
			&#8216;col2&#8242;   => &#8216;test2&#8242;<br />
		);</p>
<p>$Result = $Connect->autoExecute($table, $Value, DB_AUTOQUERY_INSERT);</p>
<p>if (PEAR::isError($Result)) {<br />
    die($res->getMessage());<br />
}</p>
<p>$Connect->disconnect();
</p></blockquote>
<p>Updateしたい場合には、以下のような書き方になるようです。</p>
<blockquote><p>
$test1 = &#8220;test&#8221;;<br />
$table = &#8220;mysql_test_table&#8221;<br />
$xxxx = &#8220;xxxxx&#8221;</p>
<p>$Connect = DB::connect($Dsn);<br />
if (DB::iserror($Connect)){<br />
    die($Connect->getMessage());<br />
}</p>
<p>$Value = array(<br />
			&#8216;name&#8217;   => &#8216;hattara&#8217;,<br />
			&#8216;domain&#8217; => &#8216;teclog.hattara.info&#8217;,<br />
			&#8216;col1&#8242;   => $test1,<br />
			&#8216;col2&#8242;   => &#8216;test2&#8242;<br />
		);</p>
<p>$Where = &#8220;col1 = &#8216;abcde&#8217; AND col2 = &#8216;&#8221; . $xxxx . &#8220;&#8216;&#8221;;</p>
<p>$Result = $db->autoExecute($table, $Value, DB_AUTOQUERY_UPDATE, $Where);</p>
<p>if (PEAR::isError($Result)) {<br />
    die($res->getMessage());<br />
}</p>
<p>$Connect->disconnect();
</p></blockquote>
<p>まあ、要するに、</p>
<blockquote><p>
autoExecute(<br />
                  <テーブル名>,<br />
                  <変更カラム名と値の連想配列>,<br />
                  <処理指定>,<br />
                  < updateならwhereがあれば><br />
                );
</p></blockquote>
<p>となりますね。<処理指定>の場所には、</p>
<blockquote><p>
　　insertなら「DB_AUTOQUERY_INSERT」<br />
　　updateなら「DB_AUTOQUERY_UPDATE」
</p></blockquote>
<p>が入るわけです。</p>
<p>ただし、残念でならないのが、updateをする場合に、where句を指定できるのですが、<br />
これがプレースホルダに対応していない。<br />
なので、上記のwhereの部分で、変数部分にはエスケープ処理を入れないといけない。<br />
なんか、あと一味足りない感じですね。</p>
<p>参考元：<br />
<a href="http://sane.justblog.jp/blog/2007/06/autoprepareauto_40a7.html">「實松アウトプット」さん >> autoPrepareとautoExecuteのwhere句</a></p>
]]></content:encoded>
			<wfw:commentRss>http://teclog.hattara.info/?feed=rss2&#038;p=37</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
