2013年2月10日日曜日

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

  1. レコードの総件数を調べる
    select count(*) from users;

    チームというフィールドにはどのような値が入っているのか、重複なしで調べる
    select distinct team from users;

    数値に関しては最大値もとれる
    select max(score) from users;

    平均を取る時はこれ
    select avg(score) from users;

    合計はこれ
    select sum(score) from users;

    ある項目で指定した値に一致するもの同士の他のフィールド値の合計
    select round();
    乱数を表示 ランダムに抽出する

    select * from users orders by round() limit1;
    適当に並び替えてそのうち一人だけ持ってきなさい

    型を明示的に表示する→typeof(フィールド名)
    select name, typeof(name) from users

    select email, length(email) from users;
    選んだフィールドのある各項目の長さ

    select concat(name, '(',team,')') from users;
    文字や項目を連結して表示できる

    select concat(name, '(',team,')') as label from users;
    とすると項目名がlabelと表示される

    select name, substring(team, 1,1) from users;
    ここではチーム名の一文字目から一文字だけ引っ張ってくるように指示している

    現在時刻を求めたい時は
    select now();

    select name, month(created) from users; dagediff
    日付の差分を日数で求める

    select name, datediff(now(), created) from users;
    で現在と作られた日の差分が求められる

    selsect!!!!!!!!!!!!!!!!!!!!!!!
    リファレンスの関数と演算しというところに情報があるのでみてください
  2.  

0 件のコメント:

コメントを投稿