简体   繁体   中英

Compare 2 tables and update one table based on a condition

I have 2 tables

table1

name    phoneno    email    status

name1 11111111 email1 

name2 22222222 email2

name3 33333333 email3 

name4 44444444 email4

table2

deviceaddr

email1

email3

Now i need to compare these 2 tables and update my first table such that if table1.email=table2.deviceaddr then my table1 should be updated like shown below:

table1

name    phoneno    email    status

name1 11111111 email1 present

name2 22222222 email2

name3 33333333 email3 present

name4 44444444 email4

How to write the SQL query for this? I am using PHPMyAdmin

You can try this :

update table1
set status = 'present'
where email in (select deviceaddr from table2)

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