標籤:
進階查詢在資料庫中用得是最頻繁的,也是應用最廣泛的。
Ø 基本常用查詢
--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進階查詢