簡單查詢


查詢數據(關鍵字:select)

(1)簡單查詢

select * from 表名

select 列名 from 表名

select 列名 as 別名 from 表名

(2)條件查詢 (where  or  and)

select * from 表名 where 條件1

select * from 表名 where 條件1 or 條件2

select * from 表名 where 條件1 and 條件2

(3)范圍查詢 (between  and)

select * from 表名 where 列名 between 值1 and 值2

(4)離散查詢 (in  not in)

select * from 表名 where 列名 in(數據列表)

select * from 表名 where 列名 not in(數據列表)

(5)模糊查詢  (like   %任意多個字符    _任意一個字符)

select * from 表名 where 列名 like ‘%_’

(6)排序查詢 ( order by    desc降序   asc升序)

select * from 表名 order by 列名 ——默認升序,也可在列名后面加asc

select * from 表名 order by 列名 desc

(7)分組查詢  (group by     having)

select * from 表名 group by 列名 (要把非聚合列名都放在子查詢中) having  條件 ——having需要跟在group by 后使用

(8)分頁查詢  (top  n 取前n個值)

select  top n * from 表名

(9)去重查詢  (關鍵字: distinct )

select distinct 列名 from 表名

聚合函數(統計函數)

select count(*) from 表名

select sum(列名) from 表名

select avg(列名) from 表名

select max(列名) from 表名

(10)ROW_NUMBER() 、partition by(按列分組后排序) order by  (排序)

select ROW_NUMBER() over(order by cbatch asc)rown,cwhcode,cinvcode,iQuantity,cbatch from [UFDATA_006_2012]..currentstock where cwhcode=@cwhcode and cinvcode=@cinvcode and iQuantity>0

 1 --最高語文成績 和最低語文成績
 2 select MAX(yuscore),MIN(yuscore) from xuesheng
 3 --最高數學成績和最低數學成績
 4 select MAX(shuscore) as 數學最高分,MIN(shuscore) as 數學最低分 from xuesheng
 5 --每個班級的平均分
 6 select banji 班級,avg(shuscore) 數學平局分 from xuesheng group by banji
 7 --所有男生的姓名
 8 select name from xuesheng where sex=''
 9 --一班的數學最高分和數學最低分
10 select MAX(shuscore) as 數學最高分,MIN(shuscore) as 數學最低分 from xuesheng where banji='一班'
11 --數學成績最高的同學的信息
12 select top 1 *from xuesheng order by shuscore desc
13 --女生的人數
14 select COUNT(*) from xuesheng where sex=''
15 --平均分超過81的班級、人數
16 select banji,COUNT(*),avg(shuscore) from xuesheng group by banji having AVG(shuscore)>81
17 --數學成績大於75的 班級的人數
18 select banji,COUNT(*)from xuesheng where shuscore>75 group by banji order by COUNT(*) desc
19 --一班數學成績大於75的學生信息
20 select *from xuesheng where shuscore>75 and banji='一班'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM