2014年9月5日金曜日

PHP・MySQL 最小値・最大値

$dbh = new PDO($DSN , $DBUSER , $DBPASS);
$query="select min(id) as id_min, max(id) as id_max from test";
$stmt = $dbh->query($query);
$stmt->execute();
$result = $stmt->fetch( PDO::FETCH_ASSOC );
$min = $result['id_min'];
$max = $result['id_max'];

2014年9月4日木曜日

MySQL:文字列のスペース削除

select * from table where replace(replace(key,' ',''),' ','') like '%検索文字列%'


// 例:pdoで検索文字列(:KEY)をバインドする方法
//   ※\sは[ \t\n\f\r]と同義
//   ※utf-8の場合、preg_replaceの修飾子に'u'が必要
$stmt->bindValue(':KEY','%' . preg_replace('/[\s ]/u','',$key) . '%');