PHPMYADMIN:EXISTS 子句中的访问字段
PHPMYADMIN: Access field in EXISTS clause
我确定这只是一个打字错误,但是如何在 PHPMyAdmin 中正确编写以下查询?
SELECT DISTINCT `email_address` as tmp1
FROM `already_customer_checks`
WHERE `is_customer` = 0
AND NOT EXISTS (
SELECT *
FROM `already_customer_checks`
WHERE `email_address` = tmp1
AND `is_customer` = 1
)
Error: #1054 - Unknown table field 'tmp1' in where clause
背景:我想获取 'is_customer' = 0 的所有电子邮件地址,并且 'is_customer' = 1 的 table 中不存在其他电子邮件地址。
非常感谢您!
要使用子查询执行此操作,您需要将别名 tmp1
放在 table 上,而不是列上。然后:
SELECT DISTINCT `email_address`
FROM `already_customer_checks` as tmp1
WHERE `is_customer` = 0
AND NOT EXISTS (
SELECT *
FROM `already_customer_checks`
WHERE `email_address` = tmp1.`email_address`
AND `is_customer` = 1
)
您也可以考虑@kmoser 提出的评论,如果不太清楚,可能 更有效。根据 MySQL 文档:
A LEFT [OUTER] JOIN can be faster than an equivalent subquery because the server might be able to optimize it better—a fact that is not specific to MySQL Server alone.
但是,如果您使用@kmoser 提出的 SQL,您可能不希望将 email_address
列与 tmp1
别名。
我确定这只是一个打字错误,但是如何在 PHPMyAdmin 中正确编写以下查询?
SELECT DISTINCT `email_address` as tmp1
FROM `already_customer_checks`
WHERE `is_customer` = 0
AND NOT EXISTS (
SELECT *
FROM `already_customer_checks`
WHERE `email_address` = tmp1
AND `is_customer` = 1
)
Error: #1054 - Unknown table field 'tmp1' in where clause
背景:我想获取 'is_customer' = 0 的所有电子邮件地址,并且 'is_customer' = 1 的 table 中不存在其他电子邮件地址。
非常感谢您!
要使用子查询执行此操作,您需要将别名 tmp1
放在 table 上,而不是列上。然后:
SELECT DISTINCT `email_address`
FROM `already_customer_checks` as tmp1
WHERE `is_customer` = 0
AND NOT EXISTS (
SELECT *
FROM `already_customer_checks`
WHERE `email_address` = tmp1.`email_address`
AND `is_customer` = 1
)
您也可以考虑@kmoser 提出的评论,如果不太清楚,可能 更有效。根据 MySQL 文档:
A LEFT [OUTER] JOIN can be faster than an equivalent subquery because the server might be able to optimize it better—a fact that is not specific to MySQL Server alone.
但是,如果您使用@kmoser 提出的 SQL,您可能不希望将 email_address
列与 tmp1
别名。