简体   繁体   中英

JOIN and UPDATE a MySQL record with an ID from another table?

    AdminTable
    ----------------------------
    ID | Start Date | LoginID
    ----------------------------
    3  | 10/11      | 28
    4  | 12/12      | 89
    5  | 13/13      | 74
    ----------------------------

    LoginTable:
    -------------------------------
    ID | Email              | Name
    -------------------------------
    28 |   b@gmail.com      | Bob
    89 |   j@gmail.com      | James
    74 |   f@gmail.com      | Bimmy
    ---------------------

I want to make an update to the Login table, and change Bimmy to Jimmy. But the only value I have is the user's AdminTableID.

To reiterate: Jimmy's Admin Table ID 5. I need to find some way to use this ID (5) and match it to his record in the LoginTable where 5 joins to 74 and gives me access to that row.

I want to do something like: UPDATE LoginTable AS L SET L.Name = 'Jimmy' WHERE AdminTable.ID = 5

Any one know how to make this join?

Try this:

UPDATE LoginTable 
LEFT JOIN AdminTable ON IFNULL(LoginTable.AdminID,0) = IFNULL(AdminTable.ID,0)
SET name="Jimmy" WHERE AdminTable.ID=5;

Below query will solve this,

UPDATE LoginTable
INNER JOIN
AdminTable ON IFNULL(LoginTable.AdminID,0) = IFNULL(AdminTable.ID,0)
SET Name = 'Jimmy' 
WHERE Name = 'Bimmy'

Thanks

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