gpt4 book ai didi

SQL查询,其中列值是唯一的,不包括

转载 作者:搜寻专家 更新时间:2023-10-30 20:03:53 26 4
gpt4 key购买 nike

我有一个名为 users 的表,其中包含列

登录名
名字
姓氏
和电子邮件地址。

我要出现的是
电子邮件,它不是唯一并且不显示以“geen”和“na”开头的电子邮件地址.
有人可以帮帮我吗?

最佳答案

您可以使用 like 删除以 where 子句开头或包含特定字符串的电子邮件。

如果您只想要 email 列和重复次数,您可以使用 count() 聚合和 group by 并使用 having 子句只显示 count(*) > 1 的结果:

select email, count(*) as cnt
from users
where email not like 'green%'
and email not like 'na%'
group by email
having count(*) > 1

如果您想查看那些具有重复项的电子邮件的所有行数据,您可以使用 common table expression 以及 count(*) over() 窗口聚合函数或 row_number()

;with cte as  (
select *
, row_number() over (partition by email order by lastname, firstname) as rn
, count(*) over (partition by email) as cnt
from users
where email not like 'green%'
and email not like 'na%'
)
select *
from cte
where cnt > 1
--or email like '%[0123456789]' /* uncomment to also show emails ending in a number */
order by email, rn

关于SQL查询,其中列值是唯一的,不包括,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404718/

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