gpt4 book ai didi

mysql - 如何选择特定列与另一行的值不同的 SQL 行?

转载 作者:太空宇宙 更新时间:2023-11-03 12:33:08 25 4
gpt4 key购买 nike

我有一个包含一些列的表格,其中之一是“电子邮件”。我想选择此表中的行,其中“电子邮件”中没有重复值。

意思是如果表格是这样的:

id - email
10 - hello@hello.com
11 - bro@lift.com
12 - hello@hello.com
13 - hey@hello.com

查询将只返回 id 11 和 13,因为 10 和 12 是重复的。

最佳答案

我将推荐使用JOIN 的查询。

SELECT *
FROM tableName
WHERE email IN
(
SELECT email
FROM tableName
GROUP BY email
HAVING COUNT(*) = 1
)

或使用JOIN

SELECT  a.*
FROM tableName a
INNER JOIN
(
SELECT email
FROM tableName
GROUP BY email
HAVING COUNT(*) = 1
) b ON a.email = b.email

为了获得更好的性能,您可以在 email 列上定义索引

关于mysql - 如何选择特定列与另一行的值不同的 SQL 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com