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

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.

Related

MySQL You can't specify target table X for update in FROM clause

Any SQL gurus out there who can rewrite the following query:
UPDATE cmsTemplate
SET master = NULL
WHERE master IS NOT NULL
AND master NOT IN (SELECT nodeId
FROM
( SELECT * FROM cmsTemplate a) b
)
So that it does not produce the following error:
You can't specify target table 'cmsTemplate' for update in FROM clause
Issue documented here:
http://dev.mysql.com/doc/refman/5.6/en/update.html
Thanks,
Steve
UPDATE: Description of query
The idea of the query is to do as follows:
SET the master field TO NULL
WHERE the master field IS NOT NULL
AND WHERE the master field IS NOT EQUAL TO ANY of the values for the nodeId field records (within the same table)
You can use update with join:
update cmsTemplate c1 left join cmsTemplate c2
on c1.`master` = c2.nodeId
set c1.`master` = null
where c2.nodeId is null;

MySQL Procedure Insert only if not exists

I'm using MySQL Stored Procedures and I want to insert some rows from a table's database to another table's database through a stored procedure. More specifically from database "new_schema", table "Routers" and field "mac_address" to database "data_warehouse2", table "dim_cpe" and field "mac_address".
This is the code I used in the first insertion, that worked perfectly.
insert into data_warehouse2.dim_cpe (data_warehouse2.dim_cpe.mac_address, data_warehouse2.dim_cpe.ssid)
(select new_schema.Routers.mac_address, new_schema.Routers.ssid from new_schema.Routers, data_warehouse2.dim_cpe);
Now I have more rows in the table "Routers" to be inserted into "dim_cpe" but, since there are rows already there, I want just to insert the new ones.
As seen in other posts, I tried a where clause:
where new_schema.device_info.mac_address != data_warehouse2.dim_cpe.mac_address
and a:
on duplicate key update new_schema.Routers.mac_address = data_warehouse2.dim_cpe.mac_address"
Both didn't work. What's the best way to do this?
Thanks in advance.
You could leave the source table out of the from clause, and use a not exists clause instead:
where not exists
(select mac_address from dim_cpe mac_address = new_schema.Routers.mac_address
and ssid = new_schema.Routers.ssid)
Or you could left join and check whether the fields from dim_cpe are null:
insert into data_warehouse2.dim_cpe
(data_warehouse2.dim_cpe.mac_address, data_warehouse2.dim_cpe.ssid)
(select new_schema.Routers.mac_address, new_schema.Routers.ssid
from new_schema.Routers
left join data_warehouse2.dim_cpe on
new_schema.Routers.mac_address = data_warehouse2.dim_cpe.mac_address
and new_schema.Routers.ssid = data_warehouse2.dim_cpe.ssid
where dim_cpe.mac_address is null and dim_cpe.ssid is null);
Edit to say this is a general SQL solution. I'm not sure if there's a better MySql-specific approach to this.
Edit to show your query:
insert into data_warehouse2.dim_cpe (mac_address, ssid)
select new_schema.Routers.mac_address, new_schema.Routers.ssid
from new_schema.Routers where not exists
(select data_warehouse2.dim_cpe.mac_address from data_warehouse2.dim_cpe
where data_warehouse2.dim_cpe.mac_address = new_schema.Routers.mac_address
and data_warehouse2.dim_cpe.ssid = new_schema.Routers.ssid);

Update Mysql Query Optimisation

I must create a mysql query with a large number of queries (about 150,000)
For the moment the query is:
UPDATE table SET activated=NULL
WHERE (
id=XXXX
OR id=YYYY
OR id=ZZZZ
OR id=...
...
)
AND activated IS NOT NULL
Do you know a best way for to do that please?
If you're talking about thousands of items, an IN clause probably isn't going to work. In that case you would want to insert the items into a temporary table, then join with it for the update, like so:
UPDATE table tb
JOIN temptable ids ON ids.id = tb.id
SET tb.activated = NULL
UPDATE table
SET activated = NULL
WHERE id in ('XXXX', 'YYYY', 'zzzz')
AND activated IS NOT NULL

MySQL update query on two tables but update only one table

I have two tables:
Table1: group_name, item_name, status
Table2: group_name, geo
I want to update table1. The default status is 0. I want to update the status of table1 to 1 using a single UPDATE statement.
I want to check for each row in table1 if the group_name exists in table2. If so, I will update status to 1.
I tried this but was not able to get the correct result.
UPDATE table1
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
How can I achieve my desired result?
you can use multiple update table syntax, so your query would be:
UPDATE table1,table2
SET table1.`STATUS`=1
WHERE table2.group_name=table1.group_name
You could use a multi-table update as others have shown. But you can also do it in a slightly simpler way with a single table update statement with a subselect:
UPDATE table1
SET STATUS = 1
WHERE group_name IN (SELECT group_name FROM table2)
Note also that you don't even need the precondition that status in all rows is initially set to zero. You can update all rows to their correct values in a single UPDATE statement:
UPDATE table1
SET STATUS = group_name IN (SELECT group_name FROM table2)
the SET is his command... the actual format is :
update "tablename"
set "columnname" =
"newvalue"
[,"nextcolumn" =
"newvalue2"...]
where "columnname"
OPERATOR "value"
[and|or "column"
OPERATOR "value"];
don't have a SQL database up and running, but I believe the UPDATE command is similar to the FROM command, so i think you have to do
UPDATE table1, table2 SET table1.'status' = 1
WHERE table2.group_name=table1.group_name

SSIS - Update flag of selected rows from more than one table

I have a SSIS package that copies data from table A to table B and sets a flag in table A so that the same data is not copied subsequently. This works great by using the following as the SQL command text on the ADO Net Source object:
update transfer
set ProcessDateTimeStamp = GetDate(), LastUpdatedBy = 'legacy processed'
output inserted.*
where LastUpdatedBy = 'legacy'
and ProcessDateTimeStamp is not null
The problem I have is that I need to run a similar data copy but from two sources table, joined on a primary / foreign key - select from table A join table B update flag in table A.
I don't think I can use the technique above because I don't know where I'd put the join!
Is there another way around this problem?
Thanks
Rob.
You can use a join in an update statement.
update m
set ProcessDateTimeStamp = GetDate(),
LastUpdatedBy = 'legacy processed',
somefield = t.someotherfield
output inserted.*
from transfer t
join mytable m
on t.id = m.id
where m.LastUpdatedBy = 'legacy'
and m.ProcessDateTimeStamp is null
and t.ProcessDateTimeStamp is not null
The key is to not alias the fields on the left side of the set but to alias everything else. And use the table alias for the table you are updating after the update key word so it knows which table of the join to update.