无效的 SQL 查询
我有下一个查询,我认为这是一个有效的查询,但我不断收到错误,告诉我“WHERE em.p4 = ue.p3”存在问题 - Unknown columns 'ue.p3' in ' where 子句'。
这是查询:
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 函数内的子查询,一切正常,
请帮忙!
I have the next query that in my opinion is a valid one, but I keep getting error telling me that there is a proble on "WHERE em.p4 = ue.p3" - Unknown column 'ue.p3' in 'where clause'.
This is the query:
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
If I execute the sub-query which is inside the EXISTS function, everything is O.K.
PLEASE HELP!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误的原因是关联时只能引用一个子查询层。查看 ue 别名的定义位置,并计算 FROM 子句的数量,直到到达下一个引用。
我将您的查询重写为:
EXISTS 如果满足则返回 true - 它不会根据返回“true”的子查询进行评估。不需要您拥有的额外子查询(无论如何这都会引起问题)。
The reason for the error is that you can only reference one subquery layer down when correlating. Look at where the
ue
alias is defined, and count the number of FROM clauses until to you reach the next reference.I re-wrote your query as:
EXISTS returns true if satisfied -- it doesn't evaluate based on the subquery returning "true". There's no need for the additional subquery you have (which was causing problems anyway).
AFAIK,这种相关查询目前在 mysql 中还无法实现。连接到派生表而不是使用存在。
AFAIK, this kind of correlated query is not doable in mysql as of now. Join to a derived table as opposed to using the exists.