首页 > 解决方案 > mysql Creating a Generated Column

问题描述

I am trying to create a new column in an existing table:

ALTER TABLE `table`.`data` 
ADD COLUMN `IndSale` DECIMAL(10,2) GENERATED ALWAYS AS (sum(TotalSale / 
Quantity)) VIRTUAL AFTER `InvoiceComment`;

and I am getting the following error:

ERROR 1111: Invalid use of group function

I don't understand what function is considered "group function"?

标签: mysqlmysql-workbench

解决方案


SUM is a group/aggregate function; it is used in queries such as:

SELECT id, SUM(values) FROM aTable GROUP BY id;

Edit: After Barmar's info below, it dawned on me where to check in the docs...

Without knowing the details of your table, I am guessing something like this would be what you intented:

IndSale` DECIMAL(10,2) GENERATED ALWAYS AS TotalSale/Quantity 

推荐阅读