0

我有一个查询:

SELECT b.user_id FROM users b where b.ratings_count !=      
    (SELECT COUNT(*) FROM
      ( SELECT wi.id FROM winelist_items wi
        JOIN wine_rated wr
        ON rate_user_id = b.user_id
        AND rate_vintage_id = wi.vintage_id
        AND wi.winelist_id = (SELECT id FROM winelist w
        WHERE w.user_id = b.user_id LIMIT 1 )
        AND wi.vintage_id !=0
      )t 
    ) limit 10;  

它必须从 sql 计算的列中具有不同 rating_count 的用户中选择所有 user_id。但问题是我收到错误消息'错误代码:1054。'on 子句'中的未知列'b.user_id''。我做错了什么?非常感谢您的帮助。

4

1 回答 1

1

试试这个

 SELECT b.user_id,
     (SELECT count(wi.id)
          FROM wine_rated wr
          JOIN winelist_items wi ON wr.rate_vintage_id = wi.vintage_id
          WHERE wr.rate_user_id = b.user_id  
          AND wi.winelist_id =
              (SELECT id
               FROM winelist w
               WHERE w.user_id = wi.rate_user_id LIMIT 1)
          AND wi.vintage_id !=0 ) AS CNT
FROM users b
GROUP BY b.user_id HAVING b.ratings_count !=CNT LIMIT 10;
于 2013-05-09T06:01:54.927 回答