Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,183 members, 7,818,588 topics. Date: Sunday, 05 May 2024 at 07:21 PM

SQL (mysql) Commands To Add Two Or More Column. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / SQL (mysql) Commands To Add Two Or More Column. (1085 Views)

Select All Rows Where The Sum Of Column Equals A Value / Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql / Column Visibility And Sequence (2) (3) (4)

(1) (Reply)

SQL (mysql) Commands To Add Two Or More Column. by Nobody: 10:01am On Dec 26, 2015
I have four (4) columns in a MySQL table and I want to add three(3) of the column and set the value in the last column, but I don't know the SQL command to use.

I tried,

UPDATE tablename SET total = first_column + second_column + third_column

but it is only updating it as I run the command. I want the values to be SET anytime the records is being added.

Any idea with the right SQL command to use? Thanks.
Re: SQL (mysql) Commands To Add Two Or More Column. by Squirrel(m): 10:47am On Dec 26, 2015
GetAvenue:
I have four (4) columns in a MySQL table and I want to add three(3) of the column and set the value in the last column, but I don't know the SQL command to use.

I tried,


but it is only updating it as I run the command. I want the values to be SET anytime the records is being added.

Any idea with the right SQL command to use? Thanks.

Hello @GetAvenue,

What you require is a TRIGGER see https://en.wikipedia.org/wiki/Database_trigger. However, MySQL 5.5 which I tested with seems to have a documented issue of locking tables and thus preventing you from using triggers for your scenario. Please see http://forums.mysql.com/read.php?99,122354,122354#msg-122354. Please check the latest version of MySQL for current behaviour.

The trigger would have looked like this:

CREATE TRIGGER my_trigger
AFTER INSERT ON tablename FOR EACH ROW
UPDATE tablename SET total = first_column + second_column + third_column;


If you have the flexibility of using a different RDBMS, I strongly recommend switching to PostgreSQL. It provides comparable features to Oracle and adheres to many ANSI SQL standards.

I hope this helps
Re: SQL (mysql) Commands To Add Two Or More Column. by Nobody: 6:40am On Dec 27, 2015
I can't switch DBMS

(1) (Reply)

TECH: Google To Back Apple In Court Against FBI / Node.js From Scratch – Tutsplus / What Is Your Favourite Tech Stack And Why?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 69
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.