gpt4 book ai didi

MySQL - 有趣的搜索我遇到了问题

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

我正在尝试找到一种仅使用 MySQL 查询来提取特定数据的方法。我有一个数据库如下:

<pre>
id | number | eventdate | type | email |
-----------------------------------------------------------------------
1 | 1000 | 2014-01-01 | 5 | tom@email.com |
2 | 1001 | 0000-00-00 | 1 | tom@email.com |
3 | 1002 | 2014-01-01 | 3 | john@email.com|
4 | 1003 | 0000-00-00 | 6 | joe@email.com |
5 | 1004 | 2014-01-01 | 3 | bob@email.com |
6 | 1005 | 0000-00-00 | 1 | adam@email.com|
7 | 1006 | 2014-01-01 | 3 | beth@email.com|
</pre>

查询是要找到类型不为 1 但事件日期不等于“0000-00-00”的任何人。但是如果有重复的电子邮件地址,我们只想关注没有事件日期的地址。有道理吗?

没什么紧急的,只是有人要求我们提取数据的问题。我们设法以更长的方式手动提取数据……但是,必须有一种更简单的方法。我认为我们只是想让它变得比必须的更难......

感谢任何提示!

(注意实际的表要大得多,有很多重复的行和更多的列。这些只是我们感兴趣的列。)

最佳答案

您可以使用 group byhaving 轻松做到这一点:

select email
from table t
group by email
having sum(type = 1) = 0 and
sum(eventdate <> '0000-00-00') > 0;

having 子句中的每个子句都会测试您的一个条件。

关于MySQL - 有趣的搜索我遇到了问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23140057/

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