I am new to MySQL statements, so bear with me.
I am working with a single table that contains 3 columns.
ID, value, and report ID.
What I am looking to do is update the ‘value’ to yes of id = 2 WHEN the value is ‘yes’ for id = 1, and at the same time matching the report id is matching between id 1 and id 2. Apologies if this doesn’t make sense, please let me know if I need to clarify better.
Here are some queries I’ve attempted so far:
UPDATE table
SET value = CASE WHEN value = 'Yes' AND id = 1 AND report_id LIKE report_id
THEN 'Yes' ELSE value END
WHERE (id = 2 or id = 1);
UPDATE table
SET value = 'Yes'
WHERE (report_id = report_id
AND value = 'Yes')
AND id = 1
OR id = 2;
UPDATE table
SET value = IF(value = 'Yes' AND report_id LIKE report_id AND id = 1, 'Yes', '')
WHERE id = 2;
Example Table:
id | value | report_id |
---|---|---|
1 | yes | 1001 |
1 | no | 1002 |
1 | yes | 1003 |
2 | 1001 | |
2 | 1002 | |
3 | cat | 1001 |
5 | 1002 |
3
Answers
All your conditions are being processed on a single row, not comparing the
id=1
rows to theid=2
rows with the samereport_id
.You can use a self-join to compare different rows of the table with a relationship.
You don’t need
CASE
, you can select the rows to update using theWHERE
conditions.If I understood correctly, please try to use something like below to get the result. You need to use the same table in exists condition as well. I hope my answer helps you.