0

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 ,

David12123
  • 119
  • 4
  • 15
  • 1
    table1.IP must be null, alter the column from not null to null. As changeip.New_IP contains null values – AmmyTech Aug 08 '18 at 12:55
  • Seems to be a typo aswell, " WHERE table1.SIM_NEW = hangeip.SIM_Number " – warbio Aug 08 '18 at 12:58
  • what should happen if an IP is null in the changeip table? Should it get updated to null or stay with the old IP? – isaace Aug 08 '18 at 13:10
  • @Kool-Mind, but table1.IP has a value inside ... I want to overwrite it - all this colom is fill with data . there is no empty cell there. and about what isaace say - can't be null - this is a 100% table with data , no empty\null in the changeip.IP table – David12123 Aug 08 '18 at 14:56

4 Answers4

0
ALTER table1 MODIFY IP varchar(20) null;

alter the column from not null to null if the other table consist null values

AmmyTech
  • 738
  • 4
  • 10
0

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
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

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
);
isaace
  • 3,336
  • 1
  • 9
  • 22
  • I get this error : Error Code: 1048. Column 'IP' cannot be null. , which can't be becasue all of table1.IP have data inside - I'm replacing old IP with new IP. – David12123 Aug 09 '18 at 07:21
0

An easy way to achieve the required result is as follows:

Based on your data the start tables are

enter image description here

and

enter image description here

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:

enter image description here

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.

Clinton
  • 1,111
  • 1
  • 14
  • 21