我正在尝试使用子查询中多列的数据连续更新一行中的多列。
以下方法对我不起作用,我找不到适合我需要的方法:
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
我没有收到这个错误,但有5个警告并且行没有更新:
Query OK, 0 rows affected, 5 warnings (0.01 sec)
显示警告给了我:
+-------+------+----------------------------------------------------+
| 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 |
+-------+------+----------------------------------------------------+
我知道这个问题与数据类型有关,但数据类型是浮点数,我相信这也是AVG的结果:
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 | |
+-------------+---------------+------+-----+---------+----------------+
下一个查询略有不同:
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
我得到的错误是:
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
我真的找不到办法让这个工作,我希望有人看到我做错了什么。非常感谢你提前!
答案 0 :(得分: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;
或类似的东西