私と一绪にMySQLの技术の内幕(第5版):(第1章学习日记6)

13469 ワード

1.4.9情報の取得
1.4.9.6処理日
select * from grade_event where date='2012-10-01';



select last_name,first_name,death from president where death>='1970' and death < '1980-01-01' select last_name,first_name,birth from president where monthname(birth)='March';


select last_name,first_name,birth from president where month(birth)= 3 and dayofmonth(birth) = 29;

このいくつかの例は主にいくつかの処理日によく使われる関数year(),month(),dayofmonth(),monthname(),簡単ないくつかの関数を紹介し,単語の字面意味で関数の具体的な意味を完全に理解できる.


select last_name,first_name,birth from president where month(birth)=month(curdate()) and dayofmonth(birth)=dayofmonth(curdate()) select last_name,first_name,birth,death, timestampdiff(year,birth,death)as age from president where death is not null order by age desc limit 5; select last_name,frst_namae,expiration from member where (to_days(expiration)-to_days(curdate()))<60;

Curdate()関数、戻り値が現在の日付、(システムの日付)timtstampdiff()関数、日付減算関数、この関数のパラメータ(year,birth,death)の最初のパラメータ名は減算するのがyearであり、後の2つのパラメータは減算する2つのパラメータであり、最初のパラメータ名がdayであれば、私たちは位相差を求める日付を表すことができて、todays()関数に相当しますto_days()関数の使い方を参照してください.
select date_add('1970-1-1',interval 10 year);
select date_sub('1970-1-1',interval 10 year);

yearはここでキーワードですね.月でもいいです.日数でもいいです.手でsを追加しないでください.data_add(),date_sub()は、プラス1マイナスです.1970年代に亡くなった大統領を選ぶ具体的な事例もある.
select last_name,first_name,death from president where death>='1970-1-1' and death<date_add('1970-1-1',interval 10 year);

1.4.9.7パターンマッチング
select last_name,first_name from president where last_name = 'w%';


select last_name,first_name from president where last_name like 'w%';

2つのコマンドでは、1つ目はw%に等しく、2つ目は「像」w%likeが表現のパターンマッチングであり、like「w%」はwまたはWを含むlast-nameが一致することを示します.引用符の真ん中に複数のスペースがある場合は、複数の(スペース数に等しい)文字を含むlast_name
not likeはその名の通り「似ていない」、すなわちマッチング上の考えを除いたすべての項目を返し、上の結果と完全に相補的である.
1.4.9.8カスタム変数の設定と使用
select @ Jackson_birth :=birth from president where last_name='Jackson' and first_name='Andrew'select last_name,first_name,birth from president where birth <@jackson_birth order by birth;

カスタム変数の使用方法は@変数名:=値の上の最初のクエリが主に大統領の生年月日をクエリし、変数に割り当てられる場合、このクエリは依然としてこの結果を示し、2番目のクエリは変数を参照し、presidentテーブルで目的のbirth値が変数値より小さい行を見つけます.変数をカスタマイズするときに自分の設定を表示したくない場合は、selectという方法を使わずにsetという設定を使うことができます.
set @today=curdates();
set @one_week_ago:=date_sub(@today , interval 7 days);

set文付与演算子":="と"="は、カスタム変数に注意する前に@を追加することもできます.
1.4.9.9統計の生成
このセクションの主な内容はselect文に基づく一連の統計操作であり、基本的なselectフォーマットにはほとんど差がないため、ここではこのセクションに現れるキーワード、関数について説明します.
select distinct from table;


select count(*) from tableselect count(*),count(email),count(expiration) from member;


select count (distinct state) from president;

distinctキーワードは「異なる」を表し、クエリーごとに同じ要素が1回だけカウントされ、1回返されます.count()関数、カウント関数、括弧の中に「*」があれば、カウント行数を表し、count(column)はカウントの列の異なる値が全部で何個あるかを表します.


 select sex,count(*) from student group by sex; select state,count(*)as count from president group by state order bycount desc; 

すなわち,ターゲットをグループ化してからカウントするという組合せには,多くの利点がある.統計された表に何の値があるかを事前に知らなくてもいい.クエリー文は1つだけです.3.出力をソート(order byを追加)できます.ソートに使用するカラムが関数の要約によって生成される場合、order by句で直接関数を参照することはできません.まず別名を取り、別名をorder byのターゲットにする必要があります.
select month(birth) as Month,monthname(birth) as Name, count(*) as count from president group by name order by Month;


order byを使うときにlimitで出力行の数を選択できることを知っています.今、having句を知っています.
select state,count(*) as count from president group by state order by count desc limit 4;



select state,count(*) as count from president group by state having count>1 order by count desc;

この照会は、どの州に2人以上の大統領が現れたかを教えてくれます.count()のほかに、いくつかの要約関数があります.min(),max(),sum(),avg()はそれぞれ最小値,最大値,合計値,平均値を表す.これからはクエリーをするときによく使うので、心に留めておくべきです.
このセクションでは最後にwith rollup句も紹介します.この句の意味は、クエリー統計の結果をもう一度統計することです.ここでは簡単な例があります.
select sex,count(*)from student group by sex with rollup ;

結果は男女別で、人数統計に基づいてすべての人数をまとめ、sex列はnull、count(*)列は男女数と総人数を表示します
さらにwith rollupは、次の例のような追加のスーパーセット行を生成することもできます.
id   minimun   maximum   span   total   average   count
 1     9        10        12     439     15.1379   29
 2     8        19        12     425     14.1667   30
 NUll  8        19        24     864     14.6523   59

簡単でしょう.意味がよく理解できます.この列のデフォルトの操作を1回操作しています.上記の結果が表示されるコードは次のとおりです.
select event_id,min(score) as minimum,max(score) as maximum, max(score)-min(score) as span,sum(score) as total, avg(score) as average, count(score) as count, from score group by event_id with rollup; 

1.4.9.10複数のテーブルから情報DBMSを検索する威力は、複数のテーブルからの情報を組み合わせることで、単一のテーブルでは解決できない問題を解決することができます.
select student_id,date,score,catogory from grade_event inner join score on grade_event.event_id=score.event_id where date ='2012-09-23' 

from:複数のテーブル名を指定しました.複数のテーブルから情報を取得する必要があるためです.on:grade_を指定しました.eventとscoreの接続条件、すなわちこの2つのテーブルのevent_id値は互いに一致しなければならない(ここではonの中の2つのテーブル名を書くことをお勧めします.2つのカラムの名前を書くだけでなく、このクエリ方式ではすべてのカラムのテーブル名に最初の行を含むすべてのカラムを書くことができます).
この一節は内容が多いが、多くは具体的な状況の具体的な分析であり、(left joinに言及している)私は次の章の時に具体的に言及する計画である.この小結はクエリーの論理思考の試練として、自分で読むことを提案し、真剣に体得することができる.
1.4.9ここで終わり、第1章も間もなく終わり、データベースについてさらに理解したと信じています.これからの勉强でもきっと軽车で道を熟知します~12时すぎに书き始めます~3时です~今日は少し遅いです~しかし私は坚持しました~顽张ります!