thevoipman
thevoipman

Reputation: 1833

mysql select then update / joining?

I have been reading for a few hours but my learning curve just isn't helping! I'm trying to find a few rows by doing a select statement, then when it matches, I need to grab the result and pair it up with another table then do an update. Somehow, from what I'm reading and applying, it's not helping me much.

Please kindly help me as I can't comprehend these things without seeing and applying what I'm doing... Here is my code:

select code as codea from routes where r1=1 (update plans set active=1 where code=codea) limit 100

Upvotes: 0

Views: 1004

Answers (2)

NappingRabbit
NappingRabbit

Reputation: 1918

Is this what you need?

update plans set
active = 1 
where code = (select code as codea
              from routes
              where r1=1)

Upvotes: 0

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79969

You can update with JOIN like so:

UPDATE plans p 
INNER JOIN routes r ON p.code = r.codea
SET p.active = 1 
WHERE r.r1 = 1
LIMIT 100

Upvotes: 1

Related Questions