我有一个名为用户的表,其中包含列
登录信息
名字
姓氏
和电子邮件地址。
我想要出现的是
电子邮件不唯一,并且不显示以“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/