2012年10月13日土曜日

ドットインストール MySQLの基礎 3

  1. データの挿入
    desc users; (usersはテーブル名)
    でテーブルの構造がみられる

    レコードを挿入する
    insert into users (name,email,password,score,memo,created) values ('taguchi','taguchi@gmail.com','XXX',5.5,'memomemo','2012-06-12 11:00:00');

    データの中身をみる場合は select * from users;
  2. レコードを抽出してみよう

    users からすべてのフィールドを引っ張ってきなさい
    select * from users;
     //usersはデータベース名

    users テーブルから名前とアドレスを引っ張ってきなさい
    select name, email from users;

    フィールドが多すぎる時は ユーザーごとにまとめて表示
    select * from users \G
  3. 条件付きの抽出

    select * from users where score >= 5.0;
    スコアが5以上の人を抽出

    select * from users where score = 5.5;
    スコアが5.5の人を抽出

    select * from users where score != 5.5;
    スコアが5.5以外の人を抽出

    select * from users where team <> 'red';
    チームがレッドじゃない人を抽出

    select * from users where created > '2011-06-01 11:00:00';
    この日付以降に登録した人

    select * from users where email like '%@dotinstall.com';
    このドメインを持っているひと

    select * from users where email like '%@dotinstall.__';
    @dotinstall.の後に二文字だけくるものを検索

    select * from users where score between 5.0 and 8.0
    スコアが5.0と8.0の間に入るもの  

    in チームがレッドかイエローのものを選ぶ
    select * from users where team in ('red', 'yellow');

    select * from users where score >= 4.0 and team = 'blue';
    and とorでつなげることもできる

    select * from users order by score;
    users のフィールドをscore順に並べ替える

    select * from users order by score desc;
    descとつけると大きい順に並べ替えてくれる
    文字列にも使える

    件数の制限をする
    例えば上から3つしか表示させない場合
    select * from users limit 3;

    開始位置を指定することも可能です
    開始位置はゼロから始まる
    ナンバー2から始まる二つのデーターをとってきたい場合
    select * from users limit 2, 2;

0 件のコメント:

コメントを投稿