
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to change a table (create/alter) so that the calculated “Average score” field is shown when querying the entire table without using MySQL INSERT, UPDATE?
Following is the syntax −
alter table yourTableName add column yourColumnName yourDataType generated always as ((yourColumName1+yourColumName2+....N) / N) virtual;
Let us create a table −
mysql> create table demo32 −> ( −> value1 int, −> value2 int −> ); Query OK, 0 rows affected (1.42 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo32 values(30,60); Query OK, 1 row affected (0.16 sec) mysql> insert into demo32 values(20,40); Query OK, 1 row affected (0.15 sec) mysql> insert into demo32 values(35,35); Query OK, 1 row affected (0.08 sec)
Display records from the table using select statement −
mysql> select *from demo32;
This will produce the following output −
+--------+--------+ | value1 | value2 | +--------+--------+ | 30 | 60 | | 20 | 40 | | 35 | 35 | +--------+--------+ 3 rows in set (0.00 sec)
Following is the query to a table (create/alter) so that the calculated “Average score” field is shown when querying the entire table without using INSERT, UPDATE −
mysql> alter table demo32 add column `Average Score` float −> generated always as ((value1+value2) / 2) virtual; Query OK, 0 rows affected (1.57 sec) Records: 0 Duplicates: 0 Warnings: 0
Display records from the table using select statement −
mysql> select *from demo32;
This will produce the following output −
+--------+--------+---------------+ | value1 | value2 | Average Score | +--------+--------+---------------+ | 30 | 60 | 45 | | 20 | 40 | 30 | | 35 | 35 | 35 | +--------+--------+---------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to update a timestamp field of a MySQL table?
- INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table
- Find minimum score from the entire four columns of a table in MySQL
- How can I create a table and insert values in that table using prepare statements?
- How to create a temporary MySQL table in a SELECT statement without a separate CREATE TABLE?
- Update MySQL table on INSERT command with triggers?
- How to add a calculated field to a pivot table in Excel?
- How to create a new table from the first table using MySQL LIKE?
- How to alter the database engine of a MySQL database table?
- What MySQL returns when we remove all the columns from a table by using ALTER TABLE command with DROP keyword?
- Find and Replace text in the entire table using a MySQL?
- How to change the column position of MySQL table without losing column data?
- Create a new table with the properties of an old table and without duplicates using MySQL LIKE Operator?
- How to batch update MySQL table?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?

Advertisements