I work with a complex MySQL database that collects form data. I simplified the layout in the test example table below:
|FormID|FieldName| FieldValue |
| 1 | city | Houston |
| 1 | country | USA |
| 2 | city | New York |
| 2 | country |United States|
| 3 | property| Bellagio |
| 3 | price | 120 |
| 4 | city | New York |
| 4 |zip code | 12345 |
| 5 | city | Houston |
| 5 | country | US |
Via phpMyAdmin I need to make global updates for some tables, in particular, I want to update all FieldValue entries in the "United States of America" using the FieldName "country" that have the same FormID as the FieldName "city" and FieldValue "Houston".
I can easily display these records using the SELECT statement, either using SUBQUERY or using INNER JOIN:
SELECT FieldValue
FROM test
WHERE FormID
IN (
SELECT FormID
FROM test
WHERE FieldName = "city"
AND FieldValue = "Houston"
)
AND FieldName = "country"
Or:
SELECT a.FieldValue
FROM test a
INNER JOIN test b ON a.FormID = b.FormID
WHERE a.FieldName = "country"
AND b.FieldName = "city"
AND b.FieldValue = "Houston"
UPDATE. MySQL, , join union. , .
- , ?