Rogier Willems
Rogier Willems

Reputation: 31

SQL update table2 if table1 has value X?

IF EXISTS (SELECT * FROM table1 WHERE group_id='8')
UPDATE table2 SET (...) 
WHERE usertype='numbereight'

what am i doing wrong? I need to update the usertype based on the group_id

Upvotes: 3

Views: 175

Answers (4)

Jeff
Jeff

Reputation: 918

You can join the tables and update whatever fields you need

UPDATE t1 JOIN t2 ON t1.id = t2.id
SET
  t2.field = 'whatever'
WHERE
  t1.group_id= '8' AND
  t2.usertype = 'numbereight'

Upvotes: 1

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

Try this instead:

UPDATE table2 
SET someColumn = 'somevalue'
WHERE EXISTS(SELECT * FROM table1 WHERE group_id='8')
AND usertype='numbereight'

Upvotes: 1

Steven Mastandrea
Steven Mastandrea

Reputation: 2772

Try

UPDATE table2 
SET (...) 
WHERE usertype = 'numbereight'
AND exists (select * from table1 where group_id = '8')

Upvotes: 1

aleroot
aleroot

Reputation: 72696

You can try with something like this :

UPDATE table2
SET (...) 
WHERE usertype='number eight' AND EXIST(SELECT * FROM table1 WHERE group_id='8');

Upvotes: 1

Related Questions