简体   繁体   English

在何处使用val是否有效?

[英]Is it valid to use val in where condition?

My query is 我的查询是

SELECT count(crm_clients.client_id) as count, (((sum(expectd_rev)/1000) +((COALESCE((sum(COALESCE(client_rating,0))/count(crm_client_assign.client_id)),0)*100)/5))/392.10000000)*100 as val
FROM (`crm_clients`)
JOIN `crm_project_assign` ON `crm_clients`.`client_id` = `crm_project_assign`.`client_id`
JOIN `crm_projects` ON `crm_project_assign`.`project_id` = `crm_projects`.`project_id`
LEFT JOIN `crm_client_assign` ON `crm_clients`.`client_id` = `crm_client_assign`.`client_id`
WHERE `ctype` =  2 and crm_clients.client_id in (select client_id from crm_project_assign)

group by crm_clients.client_id
order by val desc

is it possible to use val in where condition?any solution? 有可能在任何条件下使用val吗?

It is not allowable for two reasons. 不允许这样做有两个原因。 The first is that you can't use column aliases in the where clause. 首先是您不能在where子句中使用列别名。 The more important reason (in my opinion) is that val is composed of aggregation functions. (在我看来)更重要的原因是val由聚合函数组成。 You cannot use aggregation functions in the where clause. 您不能在where子句中使用聚合函数。

The solution is simple: use a having clause. 解决方法很简单:使用having子句。 This goes after the group by and you can just do: group by之后,您可以执行以下操作:

having val > 10

or whatever you would like. 或任何您想要的。

No, you can't use that alias name in the where condition. 不,您不能在where条件中使用该别名。

It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. 不允许在WHERE子句中引用列别名,因为执行WHERE子句时可能尚未确定列值。

You need to write that all again in the where condition. 您需要在where条件下重新编写所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM