よく使うコマンド(2)
■select(取り出す)
select * from テーブルの名前 where status='xxx' order by id;
statusの値がxxxデータをidの順に取り出します。order by id descだと、大きい順。
limit 2だと2個だけ。(PostgreSQL特有)
offset10だと10番目から(PostgreSQL特有)
取り出すデータは*(全部)です。(もちろん指定できる)
□便利な応用
select count(*) from テーブルの名前 where created > '2009-01-01';
2009年1月1日以降のデータの数を取得
■insert(登録する)
insert into DBの名前 values(データ1,データ2,データ3);
□実際のところ
insert into DBの名前 values(データ1,データ2,データ3);
insert into DBの名前 values(データ1,データ2,データ3);
......
とかかれたテキストファイル_insert_table1.sqlを作ってしまって、
psql -d DB名 -f _insert_table1.sql
で実行した方が楽、だし確実(実行したものが残せるし)。
■update(データ更新)
update テーブルの名前 set status = '×××' where id='xxx'
idの値がxxxの箇所のfileの値を×××にします。
■delete(データ削除)
delete from テーブルの名前 where id='xxx'
phpで登録するなら、phpの関数を使うとラクチンです。
$field_b = array(
'file',
'pdfid',
'comment',
'regdate',
'lastdate',
);
//登録データ
$tmp_b['file'] = "file";
$tmp_b['pdfid'] = $tmp_h['pdfid'];
$tmp_b['comment'] = $comment;
$tmp_b['lastdate'] = date('Y-m-d H:i:s');
$tmp_b['regdate'] = $tmp_b['lastdate'];
// DB insert
$query_b = array();
foreach($field_b as $val){
$query_b[$val] = $tmp_b[$val];
}
if(!$r = pg_insert($conn,'DBの名前',$query_b)) {
print "データが挿入できませんでした。";
}
コメントする