Table A has three columns; an IP address(varchar), lab ID(int), and origin (int).
Table B has two columns; an ID(int) and labName(varchar)
The labName is table B is made up of the IP address' first octet in A. For instance if the IP was 192.168.5.18 then there would be a lab name 'lab 192'. What I need to do is update Table A's labID column with the matching ID in table B.
I already have parsing done to build the lab name from the IP:
What I'm not sure how to do is the update based on that calculated field. Code below won't work but I'm hoping it might explain what I'm attempting to do:
UPDATE tableA
SET labID = (
SELECT labName
FROM tableB
WHERE labName = concat(‘lab ‘, substring_index(ip, “.”, 1)
)
WHERE origin = 3
MySQL will permit you to call a function (or any expression) in a join's ON
clause using a join for the table_reference
in UPDATE
syntax
UPDATE
tableA
-- Join on the IP substring expression
INNER JOIN tableB ON tableB.labName = CONCAT('lab ', substring_index(tableA.ip, '.', 1))
-- Set labID to tableB ID
SET tableA.labID = tableB.ID
WHERE origin = 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.