foreach and mysql with update

Viewed 35

I have the following problem

I make a query to my database and this is the result: Select iduser,idboton from botones

iduser idbuton
1 4
1 6
1 8
1 3
1 5
4 8
4 5
5 3
8 1
8 4

and i have another table:

idusers idbutonactive
1 4
1 6
1 12
1 3
1 7
4 4
4 2
5 3
8 7
8 4

I want to do an update of the second table according to the data of the first

UPDATE botones SET idboton=concat('9999',idboton) where idboton not in(4,6,8,3,5) and idusers not in(1,1,1,1,1)

Is it possible to go through the first table and save the data in two variables?

EXAMPLE: $one[]=(4,6,8,3,5) $two[]=(1,1,1,1,1)

UPDATE botones SET idboton=concat('9999',idboton) where idboton not in($one) and idusers not in($two)

and so with each iduser

some help?

1 Answers

If you set up an sql fiddle I will refine my answer (i.e test it) but this should work:

-- this is the inner query
SELECT idbuton FROM botones WHERE NOT (iduser=1 AND idbuton IN (4,6,8,3,5))

So let's utilize it in our "big" query

UPDATE botones SET idboton=CONCAT('9999',idboton) WHERE idboton NOT IN (
    SELECT idbuton FROM botones WHERE NOT (iduser=1 AND idbuton IN (4,6,8,3,5))
)
Related