gpt4 book ai didi

mysql - 自连接独特的行?

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

select * from hari;
+------+------------+-------+
| id | mydate | email |
+------+------------+-------+
| 1 | 2009-10-10 | 1111 |
| 1 | 2009-10-10 | 1111 |
| 1 | 2009-10-10 | 2222 |
| 2 | 2010-11-11 | 3333 |
| 2 | 2010-11-11 | 3333 |
+------+------------+-------+
5 rows in set (0.01 sec)

按 id、mydate 和 email 分组后,将只有 2 条记录 mathcing id = 1我该如何实现这一目标?

有人给了我以下查询,其中第一个 ID 的计数为 2(正确)但我发现这个查询非常令人困惑,并且猜测一定有更好的方法。

mysql>SELECT
count(sel2.refid2 ) as recCount,
refid2, recDate2
FROM
(SELECT id AS refid2, mydate AS recDate2
FROM hari AS ol_Email1
GROUP BY ol_Email1.id, ol_Email1.mydate, ol_Email1.email
) AS sel2
GROUP BY sel2.refid2
+----------+--------+------------+
| recCount | refid2 | recDate2 |
+----------+--------+------------+
| 2 | 1 | 2009-10-10 |
| 1 | 2 | 2010-11-11 |
+----------+--------+------------+
2 rows in set (0.28 sec)
<小时/>
CREATE TABLE `hari` (
`id` int(11) default NULL,
`mydate` date default NULL,
`email` varchar(100) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `hari` VALUES (1,'2009-10-10','1111'),(1,'2009-10-10','1111'),(1,'2009-10-10','2222'),(2,'2010-11-11','3333'),(2,'2010-11-11','3333');

最佳答案

SELECT * FROM hari WHERE id = 1 GROUP BY email 

关于mysql - 自连接独特的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3404790/

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