SQL Server T-SQL進階查詢

來源:互聯網
上載者:User

標籤:

進階查詢在資料庫中用得是最頻繁的,也是應用最廣泛的。

Ø 基本常用查詢

--select
select * from student;
--all 查詢所有
select all sex from student;

--distinct 過濾重複
select distinct sex from student;
--count 統計
select count(*) from student;
select count(sex) from student;
select count(distinct sex) from student;
--top 取前N條記錄
select top 3 * from student;
 
--alias column name 列重新命名
select id as 編號, name ‘名稱‘, sex 性別 from student;
--alias table name 表重新命名
select id, name, s.id, s.name from student s;

--column 列運算
select (age + id) col from student;

select s.name + ‘-‘ + c.name from classes c, student s where s.cid = c.id;

 

--where 條件
select * from student where id = 2;
select * from student where id > 7;
select * from student where id < 3;
select * from student where id <> 3;
select * from student where id >= 3;
select * from student where id <= 5;
select * from student where id !> 3;
select * from student where id !< 5;

--and 並且
select * from student where id > 2 and sex = 1;
--or 或者
select * from student where id = 2 or sex = 1;

--between ... and ... 相當於並且
select * from student where id between 2 and 5;
select * from student where id not between 2 and 5;

--like 模糊查詢
select * from student where name like  ‘%a%‘;
select * from student where name like  ‘%[a][o]%‘;
select * from student where name not like  ‘%a%‘;
select * from student where name like ‘ja%‘;
select * from student where name not like ‘%[j,n]%‘;
select * from student where name like ‘%[j,n,a]%‘;
select * from student where name like ‘%[^ja,as,on]%‘;
select * from student where name like ‘%[ja_on]%‘
--in 子查詢
select * from student where id in (1, 2);
--not in 不在其中
select * from student where id not in (1, 2);
 
--is null 是空
select * from student where age is null;
--is not null 不為空白
select * from student where age is not null;

--order by 排序
select * from student order by name;
select * from student order by name desc;
select * from student order by name asc;
--group by 分組
按照年齡進行分組統計
select count(age), age from student group by age;
按照性別進行分組統計
select count(*), sex from student group by sex;

按照年齡和性別組合分組統計,並排序
select count(*), sex from student group by sex, age order by age;
按照性別分組,並且是id大於2的記錄最後按照性別排序
select count(*), sex from student where id > 2 group by sex order by sex;
查詢id大於2的資料,並完成運算後的結果進行分組和排序
select count(*), (sex * id) new from student where id > 2 group by sex * id order by sex * id;

 

--group by all 所有分組
按照年齡分組,是所有的年齡
select count(*), age from student group by all age;
 
--having 分組過濾條件
按照年齡分組,過濾年齡為空白的資料,並且統計分組的條數和現實年齡資訊
select count(*), age from student group by age having age is not null;
按照年齡和cid組合分組,過濾條件是cid大於1的記錄
select count(*), cid, sex from student group by cid, sex having cid > 1;

按照年齡和cid組合分組,過濾條件是cid大於1的記錄
select count(*), cid, sex from student group by cid, sex having cid > 1;

按照年齡分組,過濾條件是分組後的記錄條數大於等於2
select count(*), age from student group by age having count(age) >= 2;

按照cid和性別組合分組,過濾條件是cid大於1,cid的最大值大於2
select count(*), cid, sex from student group by cid, sex having cid > 1 and max(cid) > 2;


SQL Server T-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.