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.
PHPMySQL ServerSQL

Avatar of undefined
Last Comment
et_me
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rleyba828
rleyba828
Flag of Australia image

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
Avatar of et_me
et_me

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo