0

我有下一个我认为是有效的查询,但我不断收到错误消息,告诉我“WHERE em.p4 = ue.p3”存在问题 - 'where 子句'中的未知列 'ue.p3' .

这是查询:

SELECT DISTINCT ue.p3
FROM 
    table1 AS ue INNER JOIN table2 AS e 
    ON ue.p3 = e.p3 
    WHERE 
        EXISTS(
            SELECT 1 FROM (
                SELECT (COUNT(*) >= 1) AS MinMutual 
                FROM table4 AS smm 
                WHERE 
                    smm.p1 IN (
                        SELECT sem.p3 FROM table3 AS sem 
                        INNER JOIN table2 AS em ON sem.p3 = em.p3 
                        WHERE em.p4 = ue.p3 AND 
                        sem.type = 'friends' AND em.p2 = 'normal' ) AND 
                    smm.p5 IN (
                        15000,15151
                    )
            ) AS Mutual WHERE 
        Mutual.MinMutual = TRUE) LIMIT 11

如果我执行 EXISTS 函数内部的子查询,一切正常

请帮忙!

4

2 回答 2

1

错误的原因是关联时只能向下引用一个子查询层。查看ue别名的定义位置,并计算 FROM 子句的数量,直到到达下一个引用。

我将您的查询重写为:

SELECT DISTINCT ue.p3
  FROM table1 AS ue 
  JOIN table2 AS e ON ue.p3 = e.p3 
 WHERE EXISTS(SELECT 1 AS MinMutual 
                FROM table4 AS smm 
                JOIN TABLE3 sem ON sem.p3 = smm.p1
                               AND sem.type = 'friends'
                JOIN TABLE2 em ON em.p3 = sem.p3
                              AND em.p3 = ue.p3
                              AND em.p2 = 'normal'
               WHERE smm.p5 IN (15000,15151)
            GROUP BY ? --needs a group by clause, in order to use HAVING
              HAVING COUNT(*) >= 1)  
 LIMIT 11

如果满足,EXISTS 返回 true - 它不会根据返回“true”的子查询进行评估。不需要您拥有的其他子查询(无论如何都会导致问题)。

于 2010-06-11T19:26:36.510 回答
0

AFAIK,这种相关查询目前在 mysql 中是不可行的。加入派生表,而不是使用存在。

于 2010-06-11T19:12:15.160 回答