0

我正在尝试从用户数据库中选择所有未订阅特定邮件列表的用户。如果用户是列表的成员,则内部查询会找到该用户,而外部查询会选择内部结果为零行的所有用户。

SELECT u.userid, u.username, u.mail, u.name, 
    u.lastname, u.starting_year, u.userid AS UIDD 
FROM userdb.users AS u 
WHERE (SELECT COUNT(*) FROM maildb.lists AS l, 
        maildb.list_subscriptions AS s, userdb.users AS u 
    WHERE u.userid = UIDD 
    AND l.listid = '$LIST_ID_TO_LOOKUP' 
    AND s.userid = u.userid 
    AND s.listid = l.listid) = 0 
ORDER BY u.starting_year DESC, u.lastname, u.name;

在我搬到新服务器并更新了许多组件(包括 MySQL)之前,这一切都很好。

现在运行此查询时出现错误:

#1054 - 'where 子句'中的未知列 'UIDD'

这可能是现在已弃用的旧语法的问题吗?我现在正在运行 MySQL 5.5.24。

4

2 回答 2

1

像这样,SELECT .. WHERE userid NOT IN(子查询)

SELECT 
 u.userid, 
 u.username, 
 u.mail, 
 u.name, 
 u.lastname,
 u.starting_year, 
 u.userid AS UIDD 
FROM 
 userdb.users AS u 
WHERE 
 u.userid NOT IN (
    SELECT 
      u.userid 
    FROM 
      maildb.lists AS l, 
      maildb.list_subscriptions AS s, 
      userdb.users AS u 
    WHERE 
      u.userid = u.userid 
      AND l.listid = '$LIST_ID_TO_LOOKUP' 
      AND s.userid = u.userid 
      AND s.listid = l.listid
    ) 
ORDER BY 
 u.starting_year DESC, u.lastname, u.name;
于 2012-10-23T20:20:34.750 回答
0

你不能这样做。

什么,您可以做的是重写子查询以选择列和分组依据u.userid,然后加入这个子选择sub_select.userid = u.userid

于 2012-10-23T20:00:17.070 回答