SQLタグのついている投稿
SQLで先月という表現
2009年11月16日
SQLで先月という制約を書く方法。
col BETWEEEN date_format(adddate(now(), interval -1 month),'%y-%m-01') AND last_day(adddate(now(), interval -1 month));
以下、MySQLでの実験。現在は2009年11月16日。
mysql> select '2009-10-16' between DATE_FORMAT(adddate(now(), interval -1 month),'%Y-%m-01') and last_day(adddate(now(), interval -1 month)) as result; +--------+ | result | +--------+ | 1 | +--------+ 1 row in set (0.00 sec) mysql> select '2009-10-31' between DATE_FORMAT(adddate(now(), interval -1 month),'%Y-%m-01') and last_day(adddate(now(), interval -1 month)) as result; +--------+ | result | +--------+ | 1 | +--------+ 1 row in set (0.00 sec) mysql> select '2009-10-01' between DATE_FORMAT(adddate(now(), interval -1 month),'%Y-%m-01') and last_day(adddate(now(), interval -1 month)) as result; +--------+ | result | +--------+ | 1 | +--------+ 1 row in set (0.00 sec) mysql> select '2009-11-01' between DATE_FORMAT(adddate(now(), interval -1 month),'%Y-%m-01') and last_day(adddate(now(), interval -1 month)) as result; +--------+ | result | +--------+ | 0 | +--------+ 1 row in set (0.00 sec) mysql> select '2009-09-31' between DATE_FORMAT(adddate(now(), interval -1 month),'%Y-%m-01') and last_day(adddate(now(), interval -1 month)) as result; +--------+ | result | +--------+ | 0 | +--------+ 1 row in set (0.00 sec) mysql>
都道府県マスタ・地域マスタ
2009年6月4日
- 都道府県
- 都道府県の知名度
- 地域(関東・東海とか)
の情報を定義するスキーマとデータ。
知名度は、Wikipediaにある2005年時点の人口を元にプライオリティを付けました。
数値が高いほど知名度が高いです。
SQLのデータ(MySQL用): [ダウンロード]
CREATE TABLE IF NOT EXISTS `prefectures` ( `prefectures_id` tinyint(3) unsigned NOT NULL auto_increment, `region_id` int(11) NOT NULL, `prefectures_name` varchar(20) NOT NULL, `prefectures_priority` tinyint(3) unsigned NOT NULL default '0', `prefectures_sort` tinyint(3) unsigned NOT NULL default '0', PRIMARY KEY (`prefectures_id`) ) ENGINE=MyISAM; CREATE TABLE IF NOT EXISTS `region` ( `region_id` int(11) NOT NULL auto_increment, `region_name` varchar(255) NOT NULL, `region_sort` tinyint(3) unsigned NOT NULL default '0', PRIMARY KEY (`region_id`) ) ENGINE=MyISAM;

最近のコメント