MySQL追加削除、マルチテーブル検索

35112 ワード

目次
  • データベース、データテーブル共通動作
  • 記録の添削
  • where句
  • クエリー共通
  • マルチテーブルクエリ
  • union結果セット統合
  • サブクエリ
  • join接続クエリー
  •  
    mysqlキーワードは大文字と小文字を区別しません
    データベース、データテーブルの一般的な操作
    show databases;  --        
    use db_student;  --            use        
    
    show tables;  #          
    

     
    レコードの変更削除
    select id,username,tel from tb_user;
    select * from tb_user;
    
    
    insert into tb_user (username,tel) values ('chy','188xxxxx'; 
    
    insert into tb_user (username,tel) values ('chy1,'188xxxx'),('chy2','187xxxxx');  --        ,()      
    
    insert into tb_user values ('chy','188xxxxx');  --              
    
    
    update tb_user set username='chy',age=20 where id=1
    update tb_user set age=age+2 where id=1;  --         
    
    
    delete from tb_user where id=1
    

    文字列、日付時間タイプの値を引き起こすには、mysqlは単一のインデックスをパーティション化しません.
     
    where句
    where id=1
    
    where id<>1  --  <>            
    where id!=1  --mysql    !=
    
    --  >   >=   <   <=
    
    
    where id between 1 and 10  -- [1,10],   
    
    where id in(1,3,9)  -- id=1 id=3 id=9
    
    
    where id < all(1,3,9)  -- id             
    where id < all(select id from tb_user where age<20)  --           
    
    where id <any(1,3,9)  #id              ,id   [1,8]。          
    
    
    where score>60 and score<90
    where score<60 or score>90
    where dep not in ("cs","math")  -- not   ,           
    
    
    where score is null 
    where score is not null  --     not null
    
    
    --     
    where name like ' %'  --    
    where name like '% '  --    
    where name like '% %' --   
    
    %  --        
    _  --       
    
    [abcd]  -- abcd        ,[]       ,eg. ' [  ]'     、  
    [!abcd]  --     abcd          ,eg. ' [!  ]'        、   ok
    [^abcd]  -- !     ^
    

     
    よく使うクエリー
    select distinct username from tb_user;  -- distinct   ,              ,     
    
    select price*number from tb_order_item where id=1;  --            
    
    
    
    --     :        、     。      ,  null        
    select count(*) from tb_user;  --     
    select count(username) from tb_user;  --        
    select count(distinct username) from tb_user;  --   ,          
    
    select max(age) from tb_user;
    select min(age) from tb_user;
    
    select sum(score) from tb_student;
    select avg(score) from tb_student;
    
    select first(username) from tb_user;  --        
    
    select last(username) from tb_user;  --         
    
    select sum(score)avg(score) from tb_student;  --      
    
    
    
    --       ,as    
    select if(gender,' ',' ') as gender from tb_user;  --         
    
    select ifnull(money,0) as money from tb_user;  --    ,        
    
    
    
    -- order by  ,     
    select * from tb_user order by age;  --      (asc)  
    select * from tb_user order by age desc;  -- desc  
    select * from tb_user order by name,age desc;  --        ,            ,          
    
    
    
    -- group by   ,     
    select * from tb_student group by dep;  --  dep         ,        ,    
    
    select count(*from tb_student group by dep;  --           。    group by    ,          ,       
    select avg(score) from tb_student group by class;  --          
    
    
    
    -- having  ,   group by    ,         
    select * from tb_student [where  ] group by class having class between 0 and 9; #  [1,10]      
    -- group by      where, where             ,having    where   
    -- group by         ,having            ,          
    
    
    
    --   ,          
    -- 1、where    ,eg.where id>0 and where id<10
    -- 2、limit
    -- 3、      ,        ,                 ,  、     
    
    -- limit      。oracle top
    select * from tb_user [where  ] [order by  ] limit 10;  #        10   
    select * from tb_user [where  ] [order by  ] limit 9,19;  #        [10,20]   。 +1,     
    
    
    
    --        、    
    select ... from ... [where  ]  [group by  ]  [having  ]  [order by  ]  [limit  ]
    --    select ... from ...  [where  ] ,            
    --  group by            
    -- having       ,          
    -- orderby  
    -- limit           ,      
    
    
    --    
    select ...  from ...  [where  ]  [group by  ]  [having  ]
    select ...  from ...  [where  ]  [order by  ]  [limit  ]
    
    
    --   
    select username as '   ' from tb_user;   --     ,           
    select tu.username from tb_user tu;  --    ,       
    
    

     
    マルチテーブルクエリ
    union結果セットのマージ
    複数のselectがそれぞれテーブルを調べる場合、selectが取得するフィールド数、フィールド名、フィールドタイプが同じである場合、unionを使用して複数の結果セットを1つの結果セットにマージできます.1つのselectはmレコードを返し、もう1つのselectはnレコードを返し、マージ後はm+nレコードです.
    --        id、name
    
    -- union     ,                 ,     ,distinct    
    select id,name from tb_student union [distinct] select id,name from tb_teacher;  
    
    -- all,      ,   
    select id,name from tb_student union all select id,name from tb_teacher;   
    
    --           ,    union  
    select  1 union [distinct|all] select  2 union [distinct|all] select  3 ...
    

     
    サブクエリ
    --  where      
    -- eg. 2  ,tb_dep            ,tb_student             ,            、  
    select id,name from tb_student where dep_id=(select id from tb_dep where dep_name='    '); 
    
    
    
    --  having      
    -- eg.             
    select * from tb_score group by id having score>(select avg(score) from tb_score)-- group by   ,     ,          ,              
    
    
    -- where、having     2      :any、all
    where xxx > any(   )  #       
    where xxx > all(   )  #      
    
    
    
    --  from      
    select    from (select   );  --                
    
    select    fromselect[  ] [  ]--              
    

     
    join接続クエリー
    デカルト積
    2つの集合を乗算してデカルト積を得た
    {A,B,C} * {a,b,c} = { (A,a)(A,b)(A,c)(B,a)(B,b)(B,c)(C,a)(C,b)(C,c) } 
    

    すなわち、1つの集合の各要素を別の集合の各要素に乗算する
     
    sql 99接続クエリー構文
    select   1   ,  2     from
    
     1 [  ]  [    ] join   2 [  ] on     
    
    [where  ]
    
    [group by  ]
    
    [having  ]
    
    [order by  ]
    
    [limit  ]
    

    sql 99の接続クエリには、明示的な書き方と呼ばれるキーワードjoinがあります.sql 99の前にもう一つのsql文法バージョンがあります:sql 92、その名の通り、92年に制定された標準.sql 92の接続クエリはjoinを持たず、同じ機能を有しており、現在では暗黙的な書き方と呼ばれている.
     
    接続タイプによって接続クエリーを4つに分類できます
  • クロスコネクション
  • 内接続:等値接続、非等値接続、自己接続
  • に分ける.
  • 外部接続:左外部接続(左接続)、右外部接続(右接続)、全外部接続(mysqlサポート、oracleサポート)
  • に分けられます.
  • 自然接続
  • 仮定tb_studentには30件の記録があり、tb_scoreは20件の記録がある(10人は欠試験、緩試験などの理由でしばらく成績がない)
    --     ,        ,    
    select st.*,sc.* from tb_student st cross join tb_score sc;
    select * from tb_student cross join tb_score;  --      *         
    -- 30*20=600,  600   
    
    
    
    
    --    ,     (2      )
    select * from tb_student st [inner] join tb_score sc on st.id=sc.student_id;  -- join       ,inner    
    --    20   ,     10    
    
    -- on  2       (    ),  on             。         ,   st.id>10,  where 
    
    --     :on         、  
    --      :on   >、>=、
    
    
    
    --         
    select * from tb_student st,tb_score sc where st.id=sc.student_id;  --  where       
    
    
    
    --        
    --     ,          。           ,         
    -- eg.    ,           ,      。               ,superior_id      id
    select te1.*,te2.name as superior_name from tb_employees te1 [inner] join tb_employees te2 where te1.superior_id=te2.id;
    --       ,         
    
    
    
    
    --    
    --         (    ),             (    ),   
    select * from tb_student st left [outer] join tb_score sc on st.id=sc.student_id;
    -- outer      ,left     ,right     ,full     (mysql   ,oracle  )
    
    --      join      (  ),     join      ,    2        (           |        )。
    
    
    
    
    --     
    --                   ,    2       (  、        )      
    select * from tb_student natural join tb_score;
    --        id  ,          on  1.id= 2.id
    
    
    
    
    --    ,   join  
    select  1   , 2   , 3    from  1 [  ]
    left join  2 [  ] on  1.id= 2.id
    left join  3 [  ] on  2.id= 3.id