select count(*),c_xy_bj a from z_user group by c_xy_bj 這個group by后面不能使用c_xy_bj 字段的別名a,只有外面再嵌套select查詢才能使用字段別名a
select c_xy_bj a from z_user where c_xy_bj = 'Y' 這個where后面不能使用c_xy_bj 字段的別名a,只有外面再嵌套select查詢才能使用字段別名a
同理,兩個字段加減乘除算出來的值,如果取別名,想通過別名來做查詢條件,那么也只能再外面嵌套select查詢時才能用做條件;但是如果用原字段來查詢是可以的。
select c_usernm,c_xy_bj,c_usernm||c_xy_bj ub from z_user where c_usernm||c_xy_bj = '陳明輝Y' 這個sql是可以的。
select c_usernm,c_xy_bj,c_usernm||c_xy_bj ub from z_user where ub = '陳明輝Y' 這個sql是不行的。
1.where 條件不能放在 group by后面,比如這個sql就是錯誤的:select count(*),c_xy_bj a from z_user group by c_xy_bj where c_xy_bj = 'Y'
2.group by后面只能使用having來做查詢,having查詢本來也是分組之后進行的查詢,接下來這兩個sql才是對的:select count(*),c_xy_bj a from z_user group by c_xy_bj having c_xy_bj = 'Y'
select count(*) cnt,c_xy_bj a from z_user group by c_xy_bj having count(*) = 28
3.對分組函數得到的結果進行查詢不能使用where,只能使用having,實在沒有使用group by進行分組的情況下也可以使用having來查詢分組函數得到的結果
select count(*) cnt from z_user where count(*) = 5062 這個sql是錯的
select count(*) cnt from z_user having count(*) = 5062 這個sql是對的