rleyba828
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.
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.
Another method I tried which is also failing is
THanks very much.
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
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)
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)
THanks very much.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did that and got the message below.
Open in new window
Inventory is the name of my database.