mysql SQL文法總結

來源:互聯網
上載者:User

標籤:style   ar   os   sp   strong   on   bs   ad   時間   

mysql主鍵操作

刪除表主鍵:

  alter table student drop primary key;

增加表主鍵:

  alter table student add primary key(id);

重新設定主鍵自增(前提是需要先執行刪除表主鍵語句):

  alter table student modify id bigint(20) auto_increment primary key;

 

驗證是否重名

加上limit 1 就是告訴mysql 找到即刻返回,不浪費後面的時間

  select count(*) from student where name = ‘tom‘ limit 1;

 

 

 

group by語句:按條件分成不同的組

建立customer表

id name cost  
1 tom 1000  
2 jojnny 2000  
3 tom 350  
4 bruce 730  
5 mark 3000  
6 ethan 2300  
7 bruce 1740  

查詢每個客戶花費的總額:

select  name, sum(cost) from customer group by customer;

查詢的結果為:

name sum(cost)
bruce     2470
ethan 2300
johnny 2000
mark 3000
tom 1350

 

order by語句:按條件順序排列

按花費從小到大查詢

 select *  from customer order by cost;

查詢結果為

id customer cost
3 tom   350
4 bruce 730
1 tom 1000
7 bruce 1740
2 johnny 2000
6 ethan 2300
5 mark 3000

 

group by order by組合:按分組條件分組後,再按排序條件排序

按客戶總花費從小到大排列

select  name, sum(cost) from customer group by customer order by sum(cost);

查詢結果為:

name sum(cost)
tom 1350
johnny 2000
ethan 2300
bruce 2470
mark 3000

 

 distinct語句:去除重複項

 查詢出所有的客戶名字

 select distinct name from customer;

查詢結果為:

name
tom
johnny
bruce
mark
ethan

 

mysql SQL文法總結

聯繫我們

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