MYSQL和JDBC的基礎回顧(二)

來源:互聯網
上載者:User

標籤:asc   簡單   準備工作   sele   添加   and   set   tin   表名   

   這篇內容將會緊接著上篇介紹MYSQL的查詢功能!

  以下將會以商品做案例,表為product.內有pid,pname,price,pdate建.

  查詢操作  文法:  select [distinct] *| 列名,列名 from 表名 [where條件];

  4.1  簡單查詢

     1.查詢所有商品:  select * from product;

     2.查詢商品名和價格:  select pname,price from product;

     3.查詢所有商品資訊使用表別名(多表時方便查詢):  select * from product as p;

     4.查詢商品名,使用列別名:  select pname as p from product;(as 可以省略)

     5.去掉重複值(按照價格):  select distinct(price) from product;

     6.將所有的商品價格+10進行顯示:  select pname,price+10 from product;

  4.2  條件查詢

     1.查詢商品為XX的商品資訊:  select * from product where pname = "xx";

      2.查詢商品價格大於60元的所有商品資訊:  select * from product where price>60;

      3.查詢商品名稱含有"X"字的商品資訊(模糊查詢):  select * from product where pname like‘%X%‘;

      4.查詢商品id在(3,6,9)範圍內的所有商品資訊:  select * from product where pid in (3,6,9);

      5.查詢商品名稱含有"X"字並且id為6的商品資訊:  select * from product where pname like ‘%X%‘ and pid = 6;

      6.查詢id為2或者id為6的商品資訊:  select * from product where pid = 2 or pid =6;

  4.3  排序

      1.查詢所有的商品,按價格進行排序(升,降):  select * from product order by price asc/desc;

      2.查詢名稱有"X"的商品資訊並且按照價格降序排序:  select * from product where pname like ‘%X%‘ order by price desc;

  4.4  彙總

      常用的彙總函式:  sum()求和, avg()平均, max()最大值, min()最小值, count()技術;

      注意:彙總函式不統計null值!

      1.擷取所有商品的價格的總和:  select sum(price) from product;

      2.擷取商品的平均價格:  select avg(price) from product;

      3.獲得所以商品的個數:  select count(*) from product;

  4.5  分組

      準備工作:  添加分類id :  alter table product add cid varchar(32);

            初始化資料:  update product set cid =‘1‘;

                    update product set cid =‘2‘ where pid in (5,6,7);

      1.根據cid欄位分組,分組後統計商品的個數:  select cid,count(*) from product group by cid;

      2.根據cid分組,分組統計每組商品的平均價格,平且平均價格大於2000元:  select avg(price) from product group by cid having avg(price)>2000;

  4.6  查詢總結

      順序:  select(一般在的後面的內容都是要查詢的欄位) - from(查詢的表) - where - group by - having(分組後帶有條件只能使用having) - order by(必須放最後)

  MYSQL的基礎總結到這就結束了,進階內容的話以後會專門開專題來編寫!下面開始JDBC篇的基礎總結!

              

MYSQL和JDBC的基礎回顧(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.