SQL执行顺序

1.from子句用来组装不同数据源的数据
2,where子句用于基于指定条件的筛选
3.group by 子句用来将数据划分为多个分组
3.1:Distinct 对数据进行去重:注意Distinct 会改变结果集的的名称:所以要根椐
如:
错:select  DISTINCT convert(datetime,Convert(char,modle.lotteryTime,111),111) from game_draw_date as modle group by  modle.lotteryTime  order by   modle.lotteryTime me  desc

正确:select  DISTINCT (convert(datetime,Convert(char,modle.lotteryTime,111),111)) as tiemKey from game_draw_date as modle group by  modle.lotteryTime  order by   tiemKey   desc

4,使用聚集函数进行计算():聚合函数对一组值执行计算并返回单一的值:如果有分组则对每组进行统计,如果没有分组则全个结果集进行统计(可以理解成:分组后每一个组相当于一个结果集)
count():满足条件的记录数

max():最大值。如果是字符串则按拼音排序
min():
sum():只能对数值类型做计算
avg():
5.使用having子句对分组进行筛选
7.使用order by 对所有结果集进行排序
而你的top 和like 都是属于where语句里面的,所以何时执行,一目了然
 

 

 

select * from game_draw_date

select * from game_draw_date as model where model.lotteryTime<'2014-12-26 15:00:00.000'

select model.lotteryTime from game_draw_date as model where model.lotteryTime<'2014-12-26 15:00:00.000' group by model.lotteryTime

select DISTINCT convert(datetime,Convert(char,modle.lotteryTime,111),111) as timeKey from game_draw_date as modle
group by modle.lotteryTime order by timeKey desc

select DISTINCT convert(datetime,Convert(char,modle.lotteryTime,111),111) as timeKey , SUM(modle.id) from game_draw_date as modle
group by modle.lotteryTime

select DISTINCT convert(datetime,Convert(char,modle.lotteryTime,111),111) as timeKey , SUM(modle.id) as sumId from game_draw_date as modle
group by modle.lotteryTime  HAVING SUM(modle.id)>10000

select DISTINCT convert(datetime,Convert(char,modle.lotteryTime,111),111) as timeKey , SUM(modle.id) as sumId from game_draw_date as modle
group by modle.lotteryTime HAVING SUM(modle.id)>10000 order by sumId desc

 

 

posted on 2015-03-31 11:35  高达  阅读(108)  评论(0)    收藏  举报

导航