在SQL语句中,select,where,group by,having,order by,这几个关键字程序执行顺序是怎样的?求指点。
比如这个查询语句:selectT1.USERNAMEAS'员工姓名',T2.DEPTNAMEas'部门名称',sum(T3.BILLMNY)AS'出账收入',sum(T4...
比如这个查询语句:
select T1.USERNAME AS '员工姓名',T2.DEPTNAME as '部门名称',sum(T3.BILLMNY) AS '出账收入',sum(T4.LOSTMNY) AS '欠费',sum(T5.PLANMNY) AS '计划值',(SUM(T3.BILLMNY)-SUM(T4.LOSTMNY) )/SUM(T5.PLANMNY) AS '完成率'
from USERINFO AS T1,DEPT AS T2, BILLMNY AS T3,LOSTMNY AS T4,PLANMNY AS T5, DEVPEPO AS T6
where T1.USERID=T5.USERID AND T1.USERID=T6.USERID and T6.DEVPEPO=T3.DEVPEPO AND T6.DEVPEPO=T4.DEVPEPO AND T1.DEPTID=T2.DEPTID AND T5.PLANMNY<>0
group by T1.USERNAME,T2.DEPTNAME
order by (SUM(T3.BILLMNY)-SUM(T4.LOSTMNY) )/SUM(T5.PLANMNY) 展开
select T1.USERNAME AS '员工姓名',T2.DEPTNAME as '部门名称',sum(T3.BILLMNY) AS '出账收入',sum(T4.LOSTMNY) AS '欠费',sum(T5.PLANMNY) AS '计划值',(SUM(T3.BILLMNY)-SUM(T4.LOSTMNY) )/SUM(T5.PLANMNY) AS '完成率'
from USERINFO AS T1,DEPT AS T2, BILLMNY AS T3,LOSTMNY AS T4,PLANMNY AS T5, DEVPEPO AS T6
where T1.USERID=T5.USERID AND T1.USERID=T6.USERID and T6.DEVPEPO=T3.DEVPEPO AND T6.DEVPEPO=T4.DEVPEPO AND T1.DEPTID=T2.DEPTID AND T5.PLANMNY<>0
group by T1.USERNAME,T2.DEPTNAME
order by (SUM(T3.BILLMNY)-SUM(T4.LOSTMNY) )/SUM(T5.PLANMNY) 展开
展开全部
以下为我理解的:
select 你要找的结果
where 找结果的条件
group by 用那些来给结果分组
having 和where 差不过也是条件
order by 给结果排序
select 你要找的结果
where 找结果的条件
group by 用那些来给结果分组
having 和where 差不过也是条件
order by 给结果排序
追问
这些关键字在程序中的执行顺序是怎样的?谁先谁后?
追答
select 是结果无所谓先後顺序没有句子就是错的,
where>group by>order by
where >having(两者都是条件)
必要性:select 必须有,where,group by having ,order by都可以不要
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询