1

我正在尝试开始工作的更新查询有一个烦人的问题...以下语句应该将 channels.media_view_count 更新为子查询的结果(适用于所有频道)。

UPDATE channels c
SET c.media_view_count = (
    SELECT SUM(t.view_count)
    FROM (
      SELECT DISTINCT m.viewkey, m.view_count
      FROM media m
      INNER JOIN participants p ON m.id = p.medium_id
      WHERE p.user_id = c.id AND m.is_viewable = 1
            AND (p.pending = 0)
      ) AS t
  );

子查询独立工作正常(为 c.id 指定实际 id 时,如 47778 或其他),但是当我执行此语句时,我得到:

ERROR 1054 (42S22): Unknown column 'c.id' in 'where clause'

我以为我可以从子查询中访问通道表(别名为 c)?我错过了什么还是我完全错了?

任何和所有的帮助表示赞赏:)

谢谢,

  • 杰夫
4

1 回答 1

8
UPDATE channels c, (
    SELECT t.user_id, SUM(t.view_count) cnt
    FROM (
      SELECT DISTINCT p.user_id, m.viewkey, m.view_count
      FROM media m
      INNER JOIN participants p ON m.id = p.medium_id
      WHERE m.is_viewable = 1
            AND (p.pending = 0)
      ) AS t GROUP BY t.user_id ) temp
SET c.media_view_count = temp.cnt
WHERE c.id =  temp.user_id

像这样尝试......虽然没有测试它:) ..
从概念上讲,它应该可以工作

于 2013-08-09T18:46:27.643 回答