gpt4 book ai didi

mysql - SQL - 确定是否存在具有特定电子邮件的用户的订单?

转载 作者:行者123 更新时间:2023-11-29 06:07:14 25 4
gpt4 key购买 nike

我有一个停电 - 我有以下表结构:

s_order:
userID | ....

s_user:
id | email | ...

我现在遍历我的所有订单并且可以读取客户的用户 ID。我现在想检查这个客户是否有其他订单,不是通过他的 ID 而是通过他的电子邮件地址来识别的(由于迁移,这可能会发生)。

可能是,在 s_user 表中我有以下数据:

s_user:
id | email | ...
1 | hello@domain.com
2 | hello@domain.com

有人可以帮忙吗?谢谢!

最佳答案

这是一种方法:

select u.*,
(case when exists (select 1
from s_order o2 join
s_user u2
on o2.user_id = u2.id
where u2.email = u.email and u2.id <> u.id
)
then 'Yes' else 'No'
end) as HasDuplicate
from s_user u;

但是,仅查看多个用户是否拥有相同的电子邮件可能就足够了。如果是这样,这是最简单的方法:

select u.email, group_concat(u.id) as userid
from s_user u
group by u.email
having count(*) > 1;

您可以将其限制为仅对有订单的用户开放:

select u.email, group_concat(u.id) as userid
from s_user u
where u.id in (select o.user_id from s_order)
group by u.email
having count(*) > 1;

关于mysql - SQL - 确定是否存在具有特定电子邮件的用户的订单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40718746/

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