mysql單表查詢

來源:互聯網
上載者:User

標籤:mysql

 簡單查詢:

    select * from tb_name

    select field1,field2 from tb_name;

    select * from tb_name where qualification ; 

    

例子:select  name,age from students where age+1>20; 

         select name from student where age>20 and gender=‘M‘;

         select name from student where age>20 or gender=‘M‘;

         select name from student where not age>20 and not gender=‘M‘;

         select name from student where not (age>20 or gender=‘M‘);

         select name from student where age>=20 and age<=50;或者select name from student where age between 20 and 25;

         select name from student where like name ‘Y%‘;

         

        

        

         REGEXP 或者RLIKE 後面可以跟上Regex:

         

         

               select name from student where name relike ‘^[MNY].*$‘;匹配以M或者N或者Y開頭後面任一字元的。

               

               

               

               IN的用法:年齡在18、20、25歲的同學名字

               

               

               select name from student where age in (18,20,25);

               

               顯示student表中cid是null的同學;

                     select name from student where cid is null;

                     

                     

                     不是null值的寫法:

                     select name from student where cid is not null;

                     

  根據查詢到的資料排序:

                       select name from student where cid is not null order by name;

                       

     資料存放區的三種格式:堆檔案、循序檔、hash

     

     給欄位取別名;

    select name as student_name from student where cid is not null order by name;

    

限制顯示的行數(limit);

    例子: select name as student_name from student as xiaohai  limit 2

    限制只顯示兩行。

    

    略過前兩行:

     select name as student_name from student as xiaohai limit 2,3

     略過前兩行顯示三行。

     

     彙總計算:

     

     所有同學的平均年齡:

        select avg(age) from student;

        select max(age) from student;

        select min(age) from student;  

        select sum(age) from student; 求和

        select count(age) from student;   求個數

        

  group by :分組

  

  select age,gender from student group by gender;

  按照性別把student表分組(即男,女) 顯示age gender

  

  select avg(age) from student group by gender;

  

  按照性別把表student分組,分別計算出他們的的平均值。

  

  分組的主要目的是彙總:

                   

  注意:對group by 條件進行過濾必須用having 它的作用相當於where

  

  select count(cid1) as persons,cid from student group by cid1 having persons>=2;

   having的作用是對group by的結果做再次過濾


本文出自 “wangconggao” 部落格,請務必保留此出處http://wangconggao.blog.51cto.com/3253868/1579692

mysql單表查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.