mysql學習一

來源:互聯網
上載者:User

標籤:lte   date   多個   而不是   group by   value   建立資料庫   基本   均值   

基礎

資料庫的命令:

  查看所有資料庫: show databases;

   查看當前使用的資料庫:select database();

  切換資料庫:use 資料庫名;

  建立資料庫:create database 資料庫名 charset=utf8;

  刪除資料庫:drop database 資料庫名;

資料表的命令:

  查看所有的表:show tables;

  建立表:create table 表名(id int auto_increment primary key not null,...)

  刪除表:drop table 表名;

  修改表:alter table 表名 add | change | drop 列;

資料的命令:

  查詢:select * from 表名;

  增加:insert into 表名 values(...);

  修改:update 表名 set 欄位=值 ...

  刪除:delete from 表名;

  邏輯刪除:在表中增加一個列,比如增加一個列名為isDelete的列,類型為bool類型,將需要刪除的記錄的該欄位值修改為1,而不是正真刪除該記錄。查詢時,只要查詢isDelete=0的記錄就可以了。

基本查詢:

select * from 表名;

select 列名1,列名2,... from 表名;

distinct 關鍵字 消除重複的行

select distinct 列名1,列名2,... from 表名;

條件查詢:

select * from 表名 where 條件;

模糊查詢:

like:

%:表示任意多個字元

_:表示任意一個字元

select * from 表名 where 列名 like  ‘xx%x‘;

範圍查詢:

in:表示在一個非連續的範圍內

  查詢id為1或3或4的記錄

  select * from 表名 where id in(1,3,4);

between ... and ... :表示在一個連續的範圍內

  查詢id在1到4之間的記錄

  select * from 表名 where id between 1 and 4;

空判斷:

  注意:null與‘‘(兩個單引號之間什麼都沒有,表示一個Null 字元串)是不同的

  判斷:is null

彙總:mysql中常用的5個彙總函式

  count(*):計算總行數,括弧中寫*或列名

    select count(*)  from 表名;

  max(列):表示求此列的最大值

    select  max(id) from 表名;

  min(列):表示求此列的最小值

    select min(id) from 表名;

  sum(列):表示求此列的和

    select sum(id) from 表名;

  avg(列):表示求此列的平均值

    select avg(id) from 表名;

分組:

  按照欄位分組:表示此欄位相同的資料會被放到一個組中,分組後,只能查詢出相同的資料列,對於有差異的資料列無法出現在結果集中。可以對分組後的資料進行統計,做彙總運算

  select 列1,列2,彙總 ... from 表名 group by 列1,列2...

  分組後的資料篩選:

    select 列1,列2,彙總 ... from 表名

    group by 列1,列2,列3...

    having 列1,...彙總...

  注意:having和where的區別:

    where:是對from後面指定的表進行資料篩選,屬於對未經處理資料進行篩選

    having:表示對group by(分組後)的結果集進行篩選

    原創組---where--->結果集---group--->結果集---having--->

排序:

  select * from 表名 order by 列1 asc | desc,列2 asc | desc,...

分頁:

  select * from 表名 limit start,count

  表示從start開始,擷取count條資料,start索引從0開始

  樣本:

    已知:每頁顯示m條資料,當前顯示第n頁(n從1開始)

    求總頁數:

      查詢總條數p1

      使用p1除以m得到p2

      如果整除則p2為總頁數

      如果不整除則p2+1為總頁數

    求第n頁的資料

    首先計算第n頁資料的開始索引,計算方法如下:

    n  start

    1  0       第一頁,從0開始

    2  m      第二頁,從m開始

    3  (n-1)*m  第n頁,從(n-1)*m開始  

    select * from 表名 limit (n-1)*m,m

基礎總結:

  完整的select語句的寫法:

  select distinct * from 表名

  where ...

  group by ... having ...

  order by ...

  limit start,count

  執行順序:

    from 表名

    where ...

    group by ...

    select distinct *

    having ...

    order by ...

    limit start,count

進階:關係,視圖,事務,索引

關係:

串連:

 

 

    

  

 

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.