简体   繁体   English

然后mysql中的sum列使用where子句中的结果

[英]Sum columns in mysql then use the result in where clause

I want to be able to do this: 我希望能够这样做:

SELECT dept.id, (invoices.col1 + invoices.col2 + invoices.col3) as sumTotal 
FROM dept 
     INNER JOIN invoices ON invoices.id_dept = dept.id 
WHERE sumTotal > 10000

But I am getting an unknown column on using "sumTotal". 但是我在使用“sumTotal”时得到了一个未知的专栏。

Is this possible? 这可能吗?

Use HAVING : 使用HAVING

SELECT dept.id, (invoices.col1 + invoices.col2 + invoices.col3) as sumTotal 
FROM dept 
INNER JOIN invoices
    ON invoices.id_dept = dept.id 
HAVING sumTotal > 10000

The problem is that the WHERE clause is executed before the SELECT statement. 问题是WHERE子句在SELECT语句之前执行 Therefore the sumTotal column is not yet available. 因此sumTotal列尚不可用。

The HAVING clause is executed after the SELECT statement. HAVING子句在SELECT语句之后执行 It kinds of filter the results out after you have selected everything. 在选择所有内容后,它会过滤掉结果。 Bear in mind, though, because of that using HAVING is slower. 但请记住,因为使用HAVING比较慢。 It operates on the whole set of rows. 它在整个行集上运行。

From the MySQL documentation : MySQL文档

The HAVING clause is applied nearly last, just before items are sent to the client, with no optimization. 几乎在最后一次应用HAVING子句,就在将项目发送到客户端之前,没有优化。 (LIMIT is applied after HAVING.) (在HAVING之后应用LIMIT。)

The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. SQL标准要求HAVING必须仅引用GROUP BY子句中的列或聚合函数中使用的列。 However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well. 但是,MySQL支持对此行为的扩展,并允许HAVING引用SELECT列表中的列和外部子查询中的列。


The HAVING clause can refer to aggregate functions, which the WHERE clause cannot: HAVING子句可以引用聚合函数,WHERE子句不能:

SELECT user, MAX(salary)
FROM users
GROUP BY user
HAVING MAX(salary) > 10;

Do not use HAVING for items that should be in the WHERE clause. 不要将HAVING用于应该在WHERE子句中的项目。

Or use WHERE... 或者使用WHERE ......

SELECT dept.id, (invoices.col1 + invoices.col2 + invoices.col3) as sumTotal 
  FROM dept 
  JOIN invoices ON invoices.id_dept = dept.id 
 WHERE invoices.col1 + invoices.col2 + invoices.col3 > 10000

You can also try this: 你也可以试试这个:

WITH temp AS
(
 SELECT
   dept.id,
   (invoices.col1 + invoices.col2 + invoices.col3) as sumTotal 
 FROM dept INNER JOIN invoices ON invoices.id_dept = dept.id
)
SELECT *
FROM temp
WHERE sumTotal > 10000

You have to use 'having' - 你必须使用'有' -

SELECT dept.id, (invoices.col1 + invoices.col2 + invoices.col3) as sumTotal 
FROM dept 
INNER JOIN invoices ON invoices.id_dept = dept.id 
HAVING sumTotal > 10000

I'm not sure whether you can use your aliased field or whether you have to write 'having (invoices.col1 + invoices.col2 + invoices.col3) > 10000. 我不确定你是否可以使用别名字段,或者你是否必须写'having(invoices.col1 + invoices.col2 + invoices.col3)> 10000。

The 'where' statement works in conjunction with the 'select': it filters which records are initially returned. 'where'语句与'select'一起使用:它过滤最初返回的记录。 'Having' then filters that returned dataset, normally because the 'having' condition could not be known at the time of the 'select'. 'have'然后过滤返回的数据集,通常是因为'select'时无法知道'having'条件。

To quote one documentation, 'The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.' 引用一个文档,'HAVING子句已添加到SQL,因为WHERE关键字不能与聚合函数一起使用。' Somewhere else is written ' The SQL HAVING clause is used in combination with the SQL GROUP BY clause. 其他地方写的' SQL HAVING子句与SQL GROUP BY子句结合使用。 It can be used in an SQL SELECT statement to filter the records that a SQL GROUP BY returns .' 它可以在SQL SELECT语句中用于过滤SQL GROUP BY返回的记录

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

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