我有一个停电 - 我有以下表结构:
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/