问题
I am trying to update multiple columns in a row, with data from multiple columns in a subquery.
The following approaches did not work for me, and I can't find different ones that suit my needs:
UPDATE
beers,
(SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AVG(taste) AS taste, AVG(aroma) AS aroma, AVG(overall) AS overall, beer_id FROM reviews) AS review_total
SET
beers.appearance = review_total.appearance,
beers.palate = review_total.palate,
beers.taste = review_total.taste,
beers.aroma = review_total.aroma,
beers.overall = review_total.overall
WHERE
review_total.beer_id = beers.id
AND
beers.id = 43
I don't get an error for this one, but 5 warnings and the row is not updated:
Query OK, 0 rows affected, 5 warnings (0.01 sec)
Show warnings gives me:
+-------+------+----------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------+
| Note | 1265 | Data truncated for column 'appearance' at row 9991 |
| Note | 1265 | Data truncated for column 'palate' at row 9991 |
| Note | 1265 | Data truncated for column 'taste' at row 9991 |
| Note | 1265 | Data truncated for column 'aroma' at row 9991 |
| Note | 1265 | Data truncated for column 'overall' at row 9991 |
+-------+------+----------------------------------------------------+
I know this issue has to do with the data type, but the data type is float, i beleive thats what AVG's result is too:
mysql> describe beers;
+-------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(90) | YES | | NULL | |
| aroma | float | YES | | NULL | |
| appearance | float | YES | | NULL | |
| palate | float | YES | | NULL | |
| taste | float | YES | | NULL | |
| overall | float | YES | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
The next query is slightly different:
UPDATE
beers
SET
beers.appearance = review_total.appearance,
beers.palate = review_total.palate,
beers.taste = review_total.taste,
beers.aroma = review_total.aroma,
beers.overall = review_total.overall
FROM
INNER JOIN (SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AVG(taste) AS taste, AVG(aroma) AS aroma, AVG(overall) AS overall, beer_id FROM reviews) review_total ON review_total.beer_id = beers.id
WHERE
beers.id = 43
The error i got for this one is:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM INNER JOIN (SELECT AVG(appearance) AS appearance, AVG(palate) AS palate, AV' at line 9
I really can't find a way to get this working and I hope someone sees what I'm doing wrong. Thank you very much in advance!
回答1:
UPDATE beers b
JOIN
( SELECT beer_id
, AVG(appearance) appearance
, AVG(palate) palate
, AVG(taste) taste
, AVG(aroma) aroma
, AVG(overall) overall
, beer_id
FROM reviews
GROUP
BY beer_id
) review_total
ON review_total.beer_id = b.id
SET b.appearance = review_total.appearance
, b.palate = review_total.palate
, b.taste = review_total.taste
, b.aroma = review_total.aroma
, b.overall = review_total.overall
WHERE b.id = 43;
or something like that
来源:https://stackoverflow.com/questions/15545131/updating-multiple-columns-with-data-from-subquery-in-mysql