MySQL:带有ifnull汇总的不明确的列

时间:2014-07-11 11:17:57

标签: mysql ambiguous rollup ifnull

当我想运行View时,我收到此错误,但我不知道原因:

[23000][1052] Column 'name' in field list is ambiguous

查询:

create view prefix_group_rating AS
SELECT

  r.rating_date                                                                                  `Month`,
  ifnull(g.`name`, 'Total')                                                                      `Group`,

  avg(r.rating)                                                                                  `Avreage Rating`,
  count(DISTINCT s.idserver)                                                                     `Server Count`,
  concat(count(DISTINCT r1.idserver), ' : ',
         concat(format(count(DISTINCT r1.idserver) / count(DISTINCT s.idserver) * 100, 2), '%')) `Rating 1`,
  concat(count(DISTINCT r2.idserver), ' : ',
         concat(format(count(DISTINCT r2.idserver) / count(DISTINCT s.idserver) * 100, 2), '%')) `Rating 2`,
  concat(count(DISTINCT r3.idserver), ' : ',
         concat(format(count(DISTINCT r3.idserver) / count(DISTINCT s.idserver) * 100, 2), '%')) `Rating 3`,
  concat(count(DISTINCT r4.idserver), ' : ',
         concat(format(count(DISTINCT r4.idserver) / count(DISTINCT s.idserver) * 100, 2), '%')) `Rating 4`,
  concat(count(DISTINCT r5.idserver), ' : ',
         concat(format(count(DISTINCT r5.idserver) / count(DISTINCT s.idserver) * 100, 2), '%'))  `Rating 5`

FROM prefix_server s
  LEFT JOIN prefix_group g
    ON s.idgroup = g.idgroup
  LEFT JOIN prefix_server_rating r
    ON s.idserver = r.idserver
  LEFT JOIN prefix_server_rating r1
    ON s.idserver = r1.idserver AND r1.rating = 1 AND r.rating_date = r1.rating_date
  LEFT JOIN prefix_server_rating r2
    ON s.idserver = r2.idserver AND r2.rating = 2 AND r.rating_date = r2.rating_date
  LEFT JOIN prefix_server_rating r3
    ON s.idserver = r3.idserver AND r3.rating = 3 AND r.rating_date = r3.rating_date
  LEFT JOIN prefix_server_rating r4
    ON s.idserver = r4.idserver AND r4.rating = 4 AND r.rating_date = r4.rating_date
  LEFT JOIN prefix_server_rating r5
    ON s.idserver = r5.idserver AND r5.rating = 5 AND r.rating_date = r5.rating_date

WHERE r.idserver_rating IS NOT null         
      AND r.rating_date IS NOT null

GROUP BY r.rating_date, g.`name`;

select * from prefix_group_rating;

当我替换“ifnull(g。name,'Total')”by“g。name”或删除“with rollup”时,我运行一个选择它有效。 请帮忙

0 个答案:

没有答案