Ram
Ram

Reputation: 387

update values in all tables using single query in mysql

I want to update multiple columns using a single query in MySql with some condition that is valid across all colums.

I have around 25 databases with the same table structure in them. I need to update a field named Status across all databases for a particular url using a single query using phpMyAdmin.

Right now I am using this query. (I need to set status=2 in table1 in all of these databases where the url is 'www.google.com')

UPDATE  `database1`.`table1` as p1,
    `database2`.`table1` as p2 ,
    `database3`.`table1` as p3
SET  p1.`STATUS` = 2,
    p2.`STATUS`= 2,
    p3.`STATUS`= 2 
WHERE p1.url='www.google.com' and
    p1.url=p2.url=p3.url

But it is updating data in all rows not only url='www.google.com'. It is updating status=2 in 'www.yahoo.com' also.

Please help me to solve this.

Upvotes: 1

Views: 4440

Answers (1)

Puggan Se
Puggan Se

Reputation: 5846

This is what you want:

UPDATE
    database1.table1 AS p1
    LEFT JOIN database2.table1 AS p2 USING (url)
    LEFT JOIN database3.table1 AS p3 USING (url)
SET
   p1.`STATUS` = 2,
   p2.`STATUS` = 2,
   p3.`STATUS` = 2
WHERE
    url = 'www.google.com';

and your didn't work becouse of p1.url=p2.url=p3.url
thats evaluated as p1.url=(p2.url=p3.url) where (p2.url=p3.url) => FALSE for all that didn't match, and then we end up with p1.url=FALSE

comparing p1.url=FALSE => comparing a string with an boolean, converting the string to an integer gives 0 for non numeric string, and converting boolean FALSE to integer gives also 0, so p1.url=FALSE

so p1.url=p2.url=p3.url is the same as NOT p2.url=p3.url

so your code update all rows where there exists a p2.url thats not equal to all p3.urls

Upvotes: 5

Related Questions