Copying value in mysql from column1 to column2 - mysql

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)

Related

How to add 2 columns in mysql?

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

MySQL how to insert one value without affecting other values

In my table "accounts" I have four columns like
user, pass, column1, column2
I need to insert value into column2, where user='special_user_value'.
How can I do this?
UPDATE accounts
SET column2 = 'New Value'
WHERE user = 'special_user_value';
You don't "insert values" into a column. You insert a row, that has a value for all the columns you specified in the table creation; Just like a real table, or excel sheet for that matter.
If you need to change a column value for a specific row, you can use UPDATE:
UPDATE table_name SET column2='new value' WHERE user='special_user_value'
This is a really basic example. If you follow the link I provided for UPDATE, you may learn more about changing table values for a specific row.
If are you looking for actually inserting a new row with a specific value for that column, there's INSERT INTO you could follow to achieve that.
Use update Query like:
Update table_nm set field1=value1, Field2=value2 Where condition;
UPDATE Accounts SET column2='NewValue' WHERE user='special_user_value'

Quickest way to duplicate a MySQL Record

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

Do I have to list all the columns in order and have values for each one in INSERT statement?

Is this possible or do I have to list all the columns?
INSERT INTO table_name (column1, **column3**, column2,...)
VALUES (value1, value2, value3,...)
Do I have to list all the columns in order and have values for each one?
You only have to put the columns you plan on inserting. The only order that needs to match up is the column name and value.
IE: 3 Columns: col1, col2,col3
INSERT INTO TABLE (col1,col2) VALUES(col1value,col2value)
INSERT INTO TABLE (col2,col3) VALUES(col2value,col3value)
INSERT INTO TABLE (col3,col2) VALUES(col3value,col2value)
For your INSERT statement it should be like this:
INSERT INTO table_name (column1, column3, column2,...) VALUES (value1, value3, value2,...)
Since you moved column3, value3 should "match up" with it
You can put your query columns in any order, as long as you specify that order like you are doing above. The actual values you insert as part of the VALUE clause must match the INSERT INTO (x,y,z) part of your query.
Any columns that you do not specify will have a default value inserted. The default value is determined by the DEFAULT value set when the column was created.
Your INSERT can fail if a column has a NOT NULL specification on it and no DEFAULT value, and you do not provide one in the query.
Your column names must correspond to the values that you are inserting. For instance, in your above query, if column1 is a varchar, column3 an int, etc, the values must correspond and be in the exact order for your query to execute successfully.

How to prepend a string to a column value in MySQL?

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.
For example, if the existing value is "try" it should become "testtry".
You can use the CONCAT function to do that:
UPDATE tbl SET col=CONCAT('test',col);
If you want to get cleverer and only update columns which don't already have test prepended, try
UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
Many string update functions in MySQL seems to be working like this:
If one argument is null, then concatenation or other functions return null too.
So, to update a field with null value, first set it to a non-null value, such as ''
For example:
update table set field='' where field is null;
update table set field=concat(field,' append');
That's a simple one
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
We can concat same column or also other column of the table.