跟随MySQL视图有问题。 Syntax check说“恭喜!SQL看起来很好”然而,我的MySQL 5.6.20给出了:
ERROR 1349 (HY000): View's SELECT contains a subquery in the FROM clause
这是视图
CREATE OR REPLACE VIEW view_salon_list AS SELECT
s.id AS salon_id,
v.name AS salon_name,
v.street AS salon_street,
v.photo AS salon_photo,
m.name AS city_name,
k.name AS county_name,
c.avg_rating AS avg_rating,
c.comment_count AS comment_count
FROM
salon_version AS v
LEFT JOIN salon AS s ON v.salonid = s.id
LEFT JOIN city AS m ON v.cityid = m.id
LEFT JOIN county AS k ON m.countyid = k.id
LEFT JOIN ( SELECT
vc.salon_id AS salon_id,
AVG(vc.rating_value) AS avg_rating,
COUNT(vc.salon_id) AS comment_count
FROM
view_comment_stats AS vc
GROUP BY
vc.salon_id
) AS c ON c.salon_id = s.id;
更新
与“重复帖子”混淆可能我是盲人,但我在FROM子句中没有子查询,因为它在帖子中标记为我的问题的答案。我只在JOIN子句中有子查询,根据这篇文章的回答Selecting multiple columns/fields in MySQL subquery,它应该是可能的。