Add column with initial value from another table - mysql

Is it possible to set initial value for newly added column from another joined table?
Something like:
ALTER TABLE atable ADD COLUMN mycolumn VARCHAR(255) NOT NULL
VALUE SELECT acolumn FROM something s WHERE s.id = atable.some_id
?

I don't think what you want is possible. Instead, use a separate update command:
ALTER TABLE atable ADD COLUMN mycolumn VARCHAR(255);
update atable a join
something s
on s.id = a.some_id
set a.mycolumn = s.acolumn;

Related

mysql copy data from table to another with the same value?

I'm trying to update my table from another table
I need to update the IP in table1 from the new_IP in table changeip according to the SIM number (whcih is the same on both tables)
I have try to do it from what it say here :
mysql update column with value from another table
I get error "IP can't be null"
this is what I wrote in the command line
UPDATE table1
SET table1.IP = (
SELECT changeip.New_IP
FROM changeip
WHERE table1.SIM_NEW = changeip.SIM_Number
);
what am I doing wrong ?
****************update
this is table1
'10.226.202.169', '8997250000031944123'
'10.226.202.170', '8997250000031944131'
'10.226.202.173', '8997250000031944164'
'10.136.136.101', '8997250400019201597'
'10.136.136.102', '8997250400019201589'
'10.136.136.103', '8997250400019201571'
'10.136.136.104', '8997250400019201563'
and so on........
this is changeip table
'10.226.202.169', '8997250000031944123', '10.136.137.221'
'10.226.202.170', '8997250000031944131', '10.136.137.222'
'10.226.202.173', '8997250000031944164', '10.136.137.223'
'10.226.202.174', '8997250000031944172', '10.136.137.224'
'10.226.202.175', '8997250000031944180', '10.136.137.225'
'10.226.202.177', '8997250000031944206', '10.136.137.226'
Thanks ,
you need join and update
UPDATE table1 t
inner join changeip p
on t.SIM_NEW= p.SIM_Number
and t.IP=p.old_ip
SET t.IP =p.New_IP
ALTER table1 MODIFY IP varchar(20) null;
alter the column from not null to null if the other table consist null values
This should work if you want to update those that have an IP on the changeip table and leaves those that don't have a new IP with the old IP:
UPDATE table1
SET table1.IP = (
SELECT changeip.New_IP
FROM changeip
WHERE table1.SIM_NEW = hangeip.SIM_Number
AND hangeip.New_IP IS NOT NULL
);
An easy way to achieve the required result is as follows:
Based on your data the start tables are
and
If you run the query
UPDATE table1, changeip
SET table1.IP = changeIP.old_IP
WHERE table1.SIM = changeip.SIM_Number
The outcome is as follows:
Which I believe is the desired result. Most of the suggestions above are missing the WHERE clause in the UPDATE statement and that is why they fail.

mysql how to change integer values in column from format of 20130401 to 0401

I would like to update the whole column from the format of yearmonthday to just monthday
for example a value in this column is 20130401 and I want to change it to 0401
If your column is a varchar column, you could use something like this:
UPDATE yourtable
SET
col = DATE_FORMAT(STR_TO_DATE(col, "%Y%m%d"), "%m%d")
or maybe just:
UPDATE yourtable
SET col = RIGHT(col, 4)
EDIT
If you need to create a new column first and then remove the old column, you could use this:
ALTER TABLE yourtable ADD COLUMN col2 VARCHAR(20);
UPDATE yourtable
SET col2 = RIGHT(col, 4);
ALTER TABLE yourtable DROP COLUMN col;
Please see fiddle here.

How to delete entries that share similar pattern in MySQL

I have a column that may contain entries like this:
abc.yahoo.com
efg.yshoo.com
hij.yahoo.com
I need to delete all the duplicates and LEAVE ONE ONLY as I don't need the others. Such command can be easily done if I know the second part (ex: yahoo.com) but my problem is that the part (yahoo.com) is not fixed. I may have entries such as:
abc.msn.com
efg.msn.com
hij.msn.com
And I want to treat all these cases at once. Is this possible?
To delete the duplicates you can use
DELETE FROM your_table t1
LEFT JOIN
(
SELECT MIN(id) AS id
FROM your_table
GROUP BY SUBSTRING_INDEX(REVERSE(col), '.', 2)
) t2 ON t2.id = t1.id
WHERE b.id IS NULL
If you need to create an UNIQUE constraint for that you can do the following
1.Add another field to hold the domain value
ALTER TABLE your_table ADD COLUMN `domain` VARCHAR(100) NOT NULL DEFAULT '';
2.Update it with the correct values
UPDATE your_table set domain = REVERSE(SUBSTRING_INDEX(REVERSE(col), '.', 2));
3.Add the unique constraint
ALTER IGNORE TABLE your_table ADD UNIQUE domain (domain);
4.Add before insert and before update trggers to set the domain column
DELIMITER $$
CREATE TRIGGER `your_trigger` BEFORE INSERT ON `your_table ` FOR EACH ROW
BEGIN
set new.domain = REVERSE(SUBSTRING_INDEX(REVERSE(new.col1), '.', 2));
END$$
CREATE TRIGGER `your_trigger` BEFORE UPDATE ON `your_table ` FOR EACH ROW
BEGIN
set new.domain = REVERSE(SUBSTRING_INDEX(REVERSE(new.col1), '.', 2));
END$$
DELIMITER ;
Note: this assumes the domain is the last 2 words when separated by '.', it will not work for a domain such as ebay.co.uk . For that you will probably need to make a stored function which returns the domain for a given host and use it instead of REVERSE(SUBSTRING_INDEX....
This is assuming that you just want to take out the letters before the first . then group on the column:
DELETE a FROM tbl a
LEFT JOIN
(
SELECT MIN(id) AS id
FROM tbl
GROUP BY SUBSTRING(column, LOCATE('.', column))
) b ON a.id = b.id
WHERE b.id IS NULL
Where id is your primary key column name, and column is the column that contains the values to group on.
This will also account for domains like xxx.co.uk where you have two parts at the end.
Make sure you have a backup of your current data or run this operation within a transaction (where you can ROLLBACK; if it didn't fit your needs).
EDIT: If after deleting the duplicates you want to replace the letters before the first . with *, you can simply use:
UPDATE tbl
SET column = CONCAT('*', SUBSTRING(column, LOCATE('.', column)))

SQL Server 2008: Update columns if its a null

I have 100 columns in my table and I want to update only columns that have NULL value.
I am comparing master table columns with temp table and trying to update master table column value with temp table column value.
Please help me in this regards.
Thanks in advance
Something like:
Update t1
set field1 = coalesce(t1.field1, 'test')
, field2 = coalesce(t1.field2, t1.field1)
, field3 = coalesce(t1.field3, t2.field1)
, field4 = coalesce(t1.field4, t2.field1, t2.field3)
FROM table1 t1
join table2 t2
on t1.someid = t2.someId
I have given you three examples of differnt ways you might update if the field is null. The first shows how to set it to a text value, the second how to set it to another field in the same table and third is the one where you get the value from a different table. The forth shows what to do if the value you are setting it to is also nul and thus want to use still another value in its place. You will need to write a coalesce update for each of the 100 columns.
The following should update a row in the master table with either its previous value (if it is not null) or the value from the temp table if the value of the master table is null.
This requires that master and temp table can be joined on a key field named Key in my case.
UPDATE m
SET
m.field1 = ISNULL(m.field1, t.field1),
...
FROM
MasterTable m
INNER JOIN TempTable t ON t.Key = m.Key
UPDATE B SET B.value = T.value
FROM
tblMaster B
INNER JOIN tblTemp T ON B.ID = T.ID
WHERE B.value IS NULL

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.