简体   繁体   中英

MYSQL UPDATING a table using Four INNER JOINS

I have a query that returns the correct values:

SELECT cnl.id         cID,
       snl.id         sID,
       cnl.fk_lei     cnl,
       sid.lei        sid,
       cnl.fk_cnty_id cCid,
       snl.fk_cnty_id sCid,
       sec.id         secID,
       sid.id         sidID
FROM   cici.name_loc cnl
       INNER JOIN se4.name_loc snl
               ON snl.legal_name = cnl.legal_name
                  AND snl.fk_cnty_id = cnl.fk_cnty_id
       INNER JOIN se4.sym_exch_cnty sec
               ON sec.id = snl.fk_sec_id
       INNER JOIN se4.identifiers sid
               ON sid.fk_sec_id = sec.id
                  AND sid.lei = 'NA'
WHERE  cnl.legal_name = 'Apple Inc.' 
+------+------+----------------------+-----+------+------+-------+-------+
| cID  | sID  | cnl                  | sid | cCid | sCid | secID | sidID |
+------+------+----------------------+-----+------+------+-------+-------+
| 2010 | 3104 | HWUPKR0MPOU8FGXBT394 | NA  |  233 |  233 | 13756 |  9722 |
+------+------+----------------------+-----+------+------+-------+-------+

I would like to modify this so that I can update a field in a table used in the query. So in the above result the field ' sid' would be updated from 'NA' to 'HWUPKR0MPOU8FGXBT394'

To be exact the 'identifiers' table has a column 'sid.lei' that needs to be changed from 'NA' to the value in the 'cnl.fk_lei' table.column - only when all of the criteria in the above select is met. I only want to change the one record, not all the records in the table.

I have tried several routes they all give the same error: ERROR 1062 (23000): Duplicate entry 'HWUPKR0MPOU8FGXBT394-NA-NA-NA-1-1' for key 'mk_ident__6fks'

This is tellin me that the update is getting it wrong.

Here is one of the updates I tried:

UPDATE identifiers AS b
       INNER JOIN cici.name_loc cnl
               ON cnl.legal_name = 'Apple Inc.'
       INNER JOIN se4.name_loc snl
               ON snl.legal_name = cnl.legal_name
                  AND snl.fk_cnty_id = cnl.fk_cnty_id
       INNER JOIN se4.sym_exch_cnty sec
               ON sec.id = snl.fk_sec_id
SET    b.lei = cnl.fk_lei
WHERE  cnl.legal_name = 'Apple Inc.' 

In this instance I am only trying to update one record. However, I have hundreds of records where snl.legal_name = cnl.legal_name and I will need to update all of them with the numbers in cnl.fk_lei.

Any ideas are most appreciated. Thanks!

I think you need to put sid.lei = 'NA' (missing) in place of cnl.legal_name = 'Apple Inc.' (already present in join condition) in where:

   UPDATE identifiers AS b
   INNER JOIN cici.name_loc cnl
           ON cnl.legal_name = 'Apple Inc.'
   INNER JOIN se4.name_loc snl
           ON snl.legal_name = cnl.legal_name
              AND snl.fk_cnty_id = cnl.fk_cnty_id
   INNER JOIN se4.sym_exch_cnty sec
           ON sec.id = snl.fk_sec_id
   SET    b.lei = cnl.fk_lei
   WHERE  sid.lei = 'NA' 

Below is UPDATE syntax from MySQL website

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

What you want to do is the following:

UPDATE identifiers AS b
   SET b.lei = (select cnl.fk_lei
                  from cici.name_loc cnl
            INNER JOIN se4.name_loc snl  ON snl.legal_name = cnl.legal_name 
                   and snl.fk_cnty_id = cnl.fk_cnty_id
            INNER JOIN se4.sym_exch_cnty sec ON sec.id = snl.fk_sec_id
                 where cnl.legal_name = 'Apple Inc.')

note that this going to update all identifiers rows, you may want to add where clause to the update to limit its impact.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM