你可以将mysql列别名传递给用户定义的函数

时间:2014-08-25 20:28:01

标签: mysql parameter-passing aliases

我有一个select语句,根据不同的标准显示多个计数

我想将计数传递给用户定义的函数(UDF)来进行一些计算

e.g。

SELECT 
player, 
COUNT(IF(action=1,1,NULL)) AS tot_bullseye, 
COUNT(IF(action=2,1,NULL)) AS tot_twentys

UDF(tot_bullseye, tot_twentys)

1 个答案:

答案 0 :(得分:1)

不,对于同一查询,永远不能在SELECTWHERE子句中访问列别名。您必须重复表达式,或使用子查询:

SELECT player, tot_bullseye, tot_twentys, UDF(tot_bullseye, tot_twentys)
FROM (SELECT player,
             COUNT(IF(action=1,1,NULL)) AS tot_bullseye,
             COUNT(IF(action=2,1,NULL)) AS tot_twentys
      ...) AS subq