Update only the rows that have columns with the same value combination of the first row - MySQL

StackOverflow https://stackoverflow.com/questions/21994315

  •  16-10-2022
  •  | 
  •  

Question

I'm sorry but I couldn't find the best words to give a title to this. But this is what's going on:

I (for example) have this table

|   P1   |   P2   |   P3   |   P4   |
|    1   |    1   |    0   |    1   |
|    0   |    1   |    0   |    0   |
|    1   |    1   |    1   |    1   |
|    0   |    0   |    1   |    1   |

And I write

SELECT * FROM Table WHERE P1!=0 OR P2!=0 OR P3!=0 OR P4!=0 LIMIT 1;

It will return to me the first row, and now I would like to UPDATE to other rows that have the same combination of the number 1 (all the rows with P1=1 and P2=1 and P4=1) by changing their numbers from 1 to 0. In this example only the 3rd row would be changed to 0010.

The tricky part is that I don't know which columns will have any of the numbers in the first row (the first combination is random). I need to get the columns with the number 1 and then update if there is other rows with the value 1 in the same column.

This is kinda messy but I hope you understand it.

In this example the final table would be:

|   P1   |   P2   |   P3   |   P4   |
|    1   |    1   |    0   |    1   |
|    0   |    1   |    0   |    0   |
|    0   |    0   |    1   |    0   |
|    0   |    0   |    1   |    1   |

To clarify:

Another random set of combinations arrives to my Table:

Table2 is:

|   P1   |   P2   |   P3   |   P4   |
|    0   |    1   |    0   |    1   |
|    1   |    1   |    0   |    1   |
|    1   |    0   |    1   |    0   |
|    0   |    1   |    1   |    1   |

With the update it would return:

|   P1   |   P2   |   P3   |   P4   |
|    0   |    1   |    0   |    1   |
|    1   |    0   |    0   |    0   |
|    1   |    0   |    1   |    0   |
|    0   |    0   |    1   |    0   |

P2 and P4 were = 1 in the first row, therefore 2nd and 4th row had the same values on those 2 columns making them valid to the update.

Was it helpful?

Solution

To avoid updating the original row and to know which is the first row, you'll need some kind of row identifier (a primary key) If you have one, the query isn't too complex, you'll join your original query with the table and use IF (or CASE) to find and update the matches;

UPDATE tablea a1
JOIN (
  SELECT * FROM TableA WHERE P1!=0 OR P2!=0 OR P3!=0 OR P4!=0 ORDER BY id LIMIT 1
) a2
ON IF(a2.p1, a1.p1, 1) AND IF(a2.p2, a1.p2, 1) AND 
   IF(a2.p3, a1.p3, 1) AND IF(a2.p4, a1.p4, 1) AND a1.id<>a2.id
SET a1.p1=IF(a2.p1, 0, a1.p1), a1.p2=IF(a2.p2, 0, a1.p2),
    a1.p3=IF(a2.p3, 0, a1.p3), a1.p4=IF(a2.p4, 0, a1.p4);

An SQLfiddle to test with.

OTHER TIPS

UPDATE yourtable
SET p1=1, p2=1, p3=0, p4=0
WHERE p1=1 and p2=1 and p4=1

? Best I can come up with given your example...

This could work by utilising MySQL vars:

Do a select specifying the row you want and store in MySQL variables;

@P1 = SELECT P1 FROM TABLE WHERE ....

same for P2 to P4

UPDATE TABLE
SET 
P1= IF(P1=@P1, NOT P1, P1),
P2= IF(P2=@P2, NOT P2, P2),
P3= IF(P3=@P3, NOT P3, P3),
P4= IF(P4=@P4, NOT P4, P4)

So if the given field in any row matches the values of row X's fields it's values are flipped(using NOT)

To avoid changing your original row add the following to the end of the query:

WHERE P1 != @P1 AND P2 != @P2 AND P3 != @P3 AND P4 != @P4 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top