mysql常用sql及常用命令

來源:互聯網
上載者:User

標籤:

一、常用sql語句:

  1、查詢:

select * from table where  1 = 1

  2、增加:

insert into table(name,age) values("張三",25)

  3、刪除:

delete from table where  1 = 1

  4、修改:

update table set field1=value1 where 1 = 1

   5、模糊查詢:like關鍵字

select * from table where name like ’%張*%’ 

  6、排序:

select * from table order by name,age [desc]

  7、計數:

select count(*) as totalcount from table

  8、求和:

select sum("tableId") as sumvalue from table

  9、平均值:

select avg("age") as avgvalue from table

  10、最大:

select max("age") as maxvalue from table

  11、最小:

select min("age") as minvalue from table
二、其他sql

  1、串連:join on    left join   right join

  表A:

a1 b1 c1
01 數學 90
02 語文 80
03 英語 70

 

  表B:

a2 b2
01 張三
02 李四
04 王五

 

select A.*,B.* from A  inner join B on(A.a1=B.a2)
a1 b1 c1 a2 b2
01 數學 90 01 張三
02 語文 80 02 李四

select A.*,B.* from A left outer join B on(A.a1=B.a2)
a1 b1 c1 a2 b2
01 數學 90 01 張三
02 語文 80 02 李四
03 英語 70 NULL NULL
select A.*,B.* from A right outer join B on(A.a1=B.a2)
a1 b1 c1 a2 b2
01 數學 90 01 張三
02 語文 80 02 李四
NULL NULL NULL 04 王五
select A.*,B.* from A  full outer join B on(A.a1=B.a2)
a1 b1 c1 a2 b2
01 數學 90 01 張三
02 語文 80 02 李四
03 英語 70 NULL NULL
NULL NULL NULL 04 王五

  2、分組 : Group by

  一張表,一旦分組完成後,查詢後只能得到組相關的資訊:(統計資訊) count,sum,max,min,avg  分組的標準)

    select             invest.projectId projectId,            invest.projectName projectName,            company.companyName companyName,            invest.investTime investTime,            invest.investMoney investMoney,            SUM(invest.investMoney) projectMoney,            CONCAT(company.area,company.industryId) projectIntroduction        FROM 
       invest JOIN company ON invest.companyId = company.companyId WHERE fundId= 1 GROUP BY projectId

  註:CONCAT為拼接字串函數。

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.