sqlserver_query3_去重查詢|模糊查詢|排序|分組|使用函數

來源:互聯網
上載者:User

標籤:添加   .com   sql   名稱   nic   values   src   alt   技術分享   

  • 時間:2017-09-29 整理:byzqy

本篇仍以“梁山好漢”資料表為例,介紹幾個常用的 T-SQL 查詢語句:

  • 去重查詢,關鍵字:distinct
  • 使用萬用字元模糊查詢,關鍵字:like,萬用字元:%
  • 排序查詢(升序、降序),關鍵字:order by、desc
  • 分組查詢,關鍵字:group by
  • 使用函數,關鍵字:max、avg
  • 規定要返回的記錄的數目,關鍵字:top

檔案:SQLQuery3.sql

/*說明: SQLQuery3.sql (這是在SQL Server Management Studio中建立的一個查詢)OS名稱: Microsoft Windows 7環境:Microsoft SQL Server 2012工具: Microsoft SQL Server Management Studio資料庫: LiangshanHero資料表: hero詳情:--    id    name    nick_name    hometown    sex        sal--    1    宋江    及時雨        山東        男        2200--    2    盧俊義    玉麒麟        河北        男        8000--    3    吳用    智多星        山東        男        1100--    4    公孫勝    入雲龍        重慶        男        5000--    4    孫二娘    母夜叉        山西        女        2000--    5    林沖    豹子頭        河南        男        2000*/--使用select進行簡單計算,as關鍵字定義新列名select 12*15select 12*13 as 計算結果--去掉重複資訊查詢select * from heroselect distinct sex from hero--設定查詢條件,使用萬用字元模糊查詢select * from hero where hometown like ‘河%‘ and sex=‘男‘--排序,升序,降序select * from heroselect * from hero order by sal,name desc--分組select name,avg(sal) as 工資 from hero where sex=‘男‘ group by nameselect sex,avg(sal) as 工資 from hero group by sex--使用函數select max(sal) as 最高工資 from heroselect top 10 sal as 前三工資 from heroselect top 3 * from hero order by salselect top 3 * from hero order by sal desc-------------------------------------------建立資料庫,建立表,添加資料create database LiangshanHerouse LiangshanHerogocreate table hero(    id int,    name varchar(50),    nick_name varchar(50),    hometown varchar(50),    sex char(2),    sal int)insert into hero values(1,‘宋江‘,‘及時雨‘,‘山東‘,‘男‘,2000)insert into hero values(2,‘盧俊義‘,‘玉麒麟‘,‘河北‘,‘男‘,8000)insert into hero values(3,‘吳用‘,‘智多星‘,‘山東‘,‘男‘,1000)insert into hero values(4,‘公孫勝‘,‘入雲龍‘,‘重慶‘,‘男‘,5000)insert into hero values(4,‘孫二娘‘,‘母夜叉‘,‘山西‘,‘女‘,2000)insert into hero values(5,‘林沖‘,‘豹子頭‘,‘河南‘,‘男‘,2000)
View Code

:

01

02

 

03

04

05

06

 

--The End.

sqlserver_query3_去重查詢|模糊查詢|排序|分組|使用函數

聯繫我們

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