I have a table called "attendance" in which there are several columns.
I want to add 2 columns and put the result in the third column in the same table.
How to do it in Mysql?
I want it to happen every time when ever a new entry comes.
UPDATED: If you have no control over INSERT statements and you want your calculated column values to be persisted then you can use a BEFORE INSERT trigger
CREATE TRIGGER tg_bi_attendance
BEFORE INSERT ON attendance
FOR EACH ROW
SET NEW.column3 = NEW.column1 + NEW.column2;
Note: you probably have to cover a case when values in column1 and/or column2 are being updated. Use a separate BEFORE UPDATEtrigger for that.
CREATE TRIGGER tg_bu_attendance
BEFORE UPDATE ON attendance
FOR EACH ROW
SET NEW.column3 = NEW.column1 + NEW.column2;
Here is SQLFiddle demo
Otherwise just calculate it on the fly in SELECT clause
SELECT column1, column2, column1 + column2 column3
FROM attendance
or create a view
CREATE VIEW vw_attendance AS
SELECT column1, column2, column1 + column2 column3
FROM attendance
To update rows that are already in the table use UPDATE
UPDATE attendance
SET column3 = column1 + column2
Related
I am trying to add columns in MySQL and dynamically populate another columns.
e.g
i have a table 'numbers'
and have column 1, column 2, column 3 and the total of these should be populated in column 4
Unless I'm misunderstanding your question, aren't you just looking for:
UPDATE numbers SET column4 = (column1 + column2 + column3);
That will update column4 in all rows of the table numbers to be the sum of the first 3 columns.
Alternatively, you could also do the addition during SELECT:
SELECT column1, column2, column3, (column1 + column2 + column3) AS column4 FROM numbers;
in which case you wouldn't need an actual column4 in the database table.
Edit:
To make it update column4 every time one of the other columns was updated you could use a trigger: https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html (There's actually an example of something very similar in the linked docs.)
Use triggers with caution though, not because there's anything inherently wrong with them — not at all — but because using them sometimes moves logic that belongs in code into a place where it can be difficult to locate, debug and keep in version control.
try
select field_1, field_2, field_3, (field_1+field_2+field_3) as field_4 from your_table
I have a table named table1 with column1(pk) and column2.
When inserting into table1 in sql, is it possible to check the value of column2 and insert the value of column1 into columns2 if the value for column2 is null?
If so how do I do it?
Yes, use an IF statment:
UPDATE table1
SET column2 = IF(column1 IS NOT NULL, column1, 'Something else')
You might want to make a BEFORE INSERT trigger for that.
Syntax: http://www.techonthenet.com/oracle/triggers/before_insert.php
Documentation: http://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html
If you don't want to make any changes to your SQL statement, you could define a BEFORE INSERT trigger on table1 that conditionally assigns a value to column2. The body of the trigger would contain a pattern like this:
IF NEW.column2 IS NULL THEN
SET NEW.column2 = NEW.column1;
END IF;
(NOTE: The value of NEW.column1 is the value supplied in the statement, or NULL if it's omitted. That is, if the value of column1 is being assigned by auto_increment, that assigned value is not available in the BEFORE INSERT trigger, so it can't be copied to column2.)
Otherwise, you would need to modify the INSERT statement itself, to use an appropriate expression to supply a value for column2, e.g.
IFNULL(:col2val,:col1val)
I have several columns in a MySQL database that I would like to add and store in another column:
column1 column2 column3 subtotal
10 10 10 30
Is there a way to accomplish this?
update yourtable set subtotal = col1 + col2 + col3
If you simply update subtotal, you have to maintain its value - ie every time one of the other columns is updated, your code has to remember to update subtotal too.
There are two ways to address this issue:
Create a trigger that fires when the row is updated. This option requires database "kung fu" and isn't immediately obvious to anyone else looking at the table the the trigger exists
Create a VIEW that does the calculation
I think option 2 is the best approach.
Here's how you code a view.
create view mytable_totalled as
select col1, col2, col3, col1 + col2 + col3 as subtotal
from mytable
Once created, the view can be used just like a table (with a few caveats), but you can certainly do any kind of select on it. The new column is calculated on the fly when selected
How about this?
UPDATE tableName SET subtotal = (column1 + column2 + column3)
Update 1
If you really don't need subtotal column in table just use query as
SELECT *, (column1 + column2 + column3) as subtotal
FROM tableName
Demo
Well if this is just a one time deal you could do an update query like so:
UPDATE MyTable set subtotal = (column1 + column2 + column3)
If you want it to be calculated on insert, you could have a calculated column.
I would personally just wait until you need it to calculate it so that information that could easily be derived from your data isn't bloating your DB.
What's the easiest way to select a row, modify couple of columns and insert it to the same table?
I'm trying to insert a new row based on another.
INSERT INTO table2 (column1, column2, column3)
SELECT column1, 'no', column3 FROM table2 WHERE column2 = 'yes'
Hopefully this is a bit clearer as to how you do this. As you can see, I've grabbed two columns from table2 and for the other column I used a text value for instead of the value for column2.
Other patterns you can use:
Combine a column and some other text (Assumes the column is already a string data type.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', column2 FROM table2 WHERE column2 = 'yes'
Combine a column and some text, One example where the column is a string and one where it is not.
INSERT INTO table2 (column1, column2)
SELECT column1 + 'no', 'A' + cast(column2 as Varchar (10)) FROM table2 WHERE column2 = 'yes'
INSERT INTO table(column1,column2) SELECT column1, const2 FROM table ...
The select list will likely mix copied columns (by name) and changed columns (by desired values).
Say column is int
INSERT INTO table(column1,column2) SELECT column1 + 1, column2 -1 FROM table
You can multiply columns and perform string functions.
Simple question, i have a query with some WHERE and i need to duplicate it with a change to 1 field to a different value? There is a unique increment ID field as well which I cannot duplicate.
Something along these lines should work. This will give you access to the row that you want to duplicate, and you just select the values to insert for the new row, replacing one of them.
INSERT tblData
( -- Lets pretend Column1 is your key
Column2
, Column3
)
SELECT
Column2
, Column3 + 5 /*Replace this with whatever value is you want*/
FROM tblData
WHERE Column1 = #Id
INSERT INTO table(field1,field2)
SELECT field1*x, field2 /*Replace accordingly*/
FROM table
WHERE key = #Id