Link to home
Start Free TrialLog in
Avatar of rleyba828
rleyba828Flag for Australia

asked on

Need help with SQL statement using update and inner join of same table

Hi Team,

   I am using mysql (mysql  Ver 14.12 Distrib 5.0.77,)  on centos 5.5.   Just need some syntax help with an update statement I need where the info it needs to update the table is within the same table itself.  I have a table of MAC addresses and you can have different MAC addresses in the same SwitchPort.  I need to populate a column called "MACS_LEARNED_ON_PORT" to total how many mac addresses are found on the same Device on the same port.  See scenario below.

DEVICENAME       SwitchPORT      MAC                     MACS_LEARNED_ON_PORT
Router-A              G0/1             aa:bb:cc:dd:01             3       
Router-A              G0/2             bb:cc:dd:ee:ff              2
Router-A              G0/3             bb:cc:dd:ee:ff              1
Router-A              G0/4             bb:cc:dd:ee:ff              1
Router-A              G0/1             bb:cc:dd:ee:02            3
Router-A              G0/2             bb:cc:dd:ee:ff             2
Router-A              G0/1             bb:cc:dd:ee:11           3

Open in new window


I was google searching and seems that to use the same table, you need to ALIAS it with another name.  However, the two methods below are failing.  What might be the correct syntax.
UPDATE MAC_SEARCH as A  INNER JOIN MAC_SEARCH as B on (A.DEVICENAME=B.DEVICENAME and A.SwitchPORT=B.SwitchPORT)
SET A.MACS_LEARNED_ON_PORT= (SELECT COUNT(MAC) from A)

Open in new window


Another method I tried which is also failing is
Update  A SET MACS_LEARNED_ON_PORT=SELECT COUNT(MAC) FROM  MAC_SEARCH A
INNER JOIN MAC_SEARCH  B   on (A.DEVICENAME=B.DEVICENAME and A.SwitchPORT=B.SwitchPORT)

Open in new window


THanks very much.
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rleyba828

ASKER

Thanks hielo,

   I did that and got the message below.

#1146 - Table 'Inventory.A' doesn't exist 

Open in new window


Inventory is the name of my database.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial