Mysql查詢——學習階段

來源:互聯網
上載者:User

標籤:效率   使用者表   des   mysq   sql語句   sql   彙總函式   改變   並且   

1、開篇

搞開發的都知道,當資料量很大的時候,我們的代碼邏輯的簡單性就顯得十分重要,否則處理起來就需要花費相當多的時間。另外還有一個地方需要注意的是我們寫的sql語句。

一個擁有多年開發的資深開發人員可以相比於剛從事開發經驗不足的開發人員寫的sql效率要高些,資料量小基本沒有什麼關係。但是假如你在擁有高資料的BAT,或者國家企業,那這

條sql的差異就明顯就大了。所以,能寫出好的sql是相當重要的。

2、簡單的查詢sql

(1)select * from pj_appoint_order;    查詢所有的約單,*表示這個表中所有的欄位

(2)select id,user_name,user_phone from pj_appoint_order;    查詢所有約單中id,user_name,user_phone這幾個欄位。

       當然你可以改變這個表中查詢出來表中欄位的名稱,例如:

       select id as ‘編號‘,user_name as ‘使用者名稱稱‘,user_phone as ‘使用者手機‘ from pj_appoint_order;

       select id  ‘編號‘,user_name  ‘使用者名稱稱‘,user_phone  ‘使用者手機‘ from pj_appoint_order;

       有as和沒有都可以。

(3)select distinct user_name from pj_appoint_order; distinct去掉重複的使用者名稱(保留一個)

(4)select * from pj_account where score>88;查詢使用者積分大於88的使用者,不包含88.

       當然,在我們mysql中
         大於符號:>
         小於符號:<
         大於等於>=
         小於等於<=
         不等於 != 或者 <>

(5)select * from stu where type =‘超級會員‘ and score >90;   查詢使用者為超級會員並且積分大於90的使用者所有資訊

(6)運算式的學習:  not , or  ,in , between  and

       select * from pj_account where score between 75 and 85;

       select * from pj_account where not type=‘超級會員‘;

       select * from pj_account where type!=‘超級會員‘;

       select * from pj_account where score = 70 or score=81 or score=59;  查詢使用者積分為70或者81或者59的使用者資訊

      ---------------------------------查詢面積為400或者500或者600的約單----------------------------------------------------------

       SELECT * FROM pj_appoint_order WHERE acreage IN(400,500,600);

       SELECT * FROM pj_appoint_order WHERE acreage = 400 OR acreage=500 OR acreage=600;

(7)like 與 萬用字元的使用
       _:佔一個字元。
       %:多個字元進行匹配
     SELECT * FROM pj_appoint_order WHERE user_name LIKE ‘%哥%‘

     SELECT * FROM pj_appoint_order WHERE user_name LIKE ‘_哥_‘

(8)一般而言查詢值為null的情況比較少,但是也是會存在的,查詢null需要用is

      select * from pj_account where score is null;

      select * from pj_account where score is not null;

(9)排序 order by   mysql預設的是升序排序(asc)

     select * from pj_account order by id desc

(10) 聚集合函式。彙總函式不對null進行操作
    count()//統計數量
    sum()//求和
    max() //最大
    min()//最小
    avg()//平均

    select count(*) from pj_account; 查詢使用者表中有多少條記錄,其他的類似。可以自己試試。

    select round( avg(ifnull(score,0)),2) from pj_account; 求平均成績。

    解析:如果積分是null則賦值為0,然後求平均值,求完平均值後保留兩位小數。

(11) where 與 having 區別
         1.     having  對聚集合函式進行條件式篩選 ,如果最後的條件是 聚集合函式 那麼我們就用 having
         2.  具體的某一條資料 
      綜合我們學習的查詢相關關鍵字:
      書寫的順序:
      select,from,where,group by,having,order by;
      執行的順序:
          from:找到我們要操作的表。
          where,group by,having, 相當與我們的條件
          select :查詢
          order by:排序。

3、一份耕耘一份收貨!兢兢業業,必有豐收。

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.