How can I automatically calculate a column in a table without using a view

For example, I have the following table:

CREATE TABLE `test` (
  `total_results` int(10) unsigned NOT NULL,
  `num_results_as_expected` unsigned int(10) NOT NULL,
) ;

I would like to add another column to the table without using VIEW to add a percentage:

(num_results_as_expected/total_results)*100

Is this possible without using VIEW?

  • Explanation

    . I know this is possible using select statent, but I would like to add this to the CREATE TABLE statement so that the column is accessible to anyone who accesses the table.

  • Clarification 2: Is a trigger the right way to do this? This will essentially store duplicate information in a column.

+3
source share
2 answers

+ " ", . :.

create trigger update_result before insert on test for each row
begin set new.result = (num_results_as_expected/total_results)*100;
+4
select (num_results_as_expected/total_results)*100 as percentage from test

, , :

select total results, num_results_as_expected, (num_results_as_expected/total_results)*100 as percentage from test

- , .

+2

Source: https://habr.com/ru/post/1705391/


All Articles