SQL 基本文法記錄

來源:互聯網
上載者:User

標籤:sql

SQL的基本文法
1:select sno,sname from student where sage between 20 and 30   ----查詢出20到30之間年紀的人員
2:select sno,sname from student where sage is null   -----從student表中查詢出年紀為空白的人員
3:select sno,sname from student where sname="cary" or sname="mina" or sname "amy"  ----查詢出cary,mina,amy的學號和     名字
     還可以使用 select sno,sname from student where sname in(‘cary‘,‘mina‘,‘amy‘) 可以實現同樣的功能

select 部分
1:AS 可以為欄位重新命名
2:distinct   去重複值
3:彙總函式()  avg(),max(),min(),sum(),count()
4:TOP  5  前5行資料  select top 5 sno,sname from student
5:Into   select top 5 into testtable from student  把查詢的TOP 5資料存到testtable表中,該testtable表存在於
      master資料庫的表中
      select sno,sname,sage,sex into test table02 from student where 1<>1  ------擷取student表結構


From 部分
   
1:表重新命名
      select bookname from book as t1,borrowbook as t2 where t1.id=t2.id  ----為book表重新命名T1 borrowbook 為t2

2:結合子查詢
      select distinct bookname from book as t1 ,(select distinct bookid from borrowbook) as t2 where t1.bookid=t2.bookid


where 部分
   1:數字 :=,>,<>,<,!=,>=,<=
   2:邏輯運算子 NOT ,OR ,AND
   3:特殊運算子:Between A and B,IN, Like

 萬用字元 %   表示任意多個字元,也可以表示0個字元
  _ 底線  表示任意單個字元
  ^ 表示取相反值  ^5  ----取不是5的值
----查詢出手機號碼134或者135開頭,倒數第四位是4或者是5的學生姓名
    select  sname from student where smobile like ‘13[45]%[45]___‘

----查詢出手機號碼倒數第四不是4或者第五位不是5的學生姓名
    select sname from student where smobile like ‘%[^45]_ _ _‘ 

 contains  包含  需要使用全文索引
    select sname from student where contains (sname,‘覃‘)  -----查詢名字包含覃的名字

select sname,max(age) from student

 

SQL語句分組與嵌套
1:group by
      select sex,count(*) from student group by sex    -----用group by 進行分組統計,group by只能包含的分組欄位和需要
      的彙總函式

 

查詢sql server的版本號碼
     select @@version

 

本文出自 “cary_qin的部落格” 部落格,請務必保留此出處http://xpqinqun.blog.51cto.com/2136/1765840

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.