gpt4 book ai didi

mysql - 查找两列中重复的 mySQL 结果 - GROUP BY 不起作用

转载 作者:行者123 更新时间:2023-11-29 11:03:53 24 4
gpt4 key购买 nike

我有一个事件列表,我想查找即将举行的事件的重复项。由于同一事件可以有多个不同的事件标题,因此我认为最安全的选择是查找同一天同一地点发生的事件,并从那里手动删除重复项。

Similar question was asked here.

$todays_date = date('Y-m-d');   
$sql="
SELECT place_name, event_title, start_date, count(*)
FROM main WHERE start_date >= '$todays_date'
GROUP BY start_date, place_name HAVING count(*) > 1";
$result = mysql_query($sql);
//display events
while ($row = mysql_fetch_array($result))
{
echo $row['place_name'] . "- ";
echo $row['event_title'] . "- ";
echo $row['start_date'] . "<br/>";
}

这根本没有给我预期的结果,事实上,我不知道这个记录集是如何组装的。

地点 - 事件名称 - 日期

Rotary Centre for the Arts- Leela Gilday - 2017-03-10
Rotary Centre for the Arts- Joe Trio - 2017-03-21
Vernon and District Performing Arts Center- Shay Keubler's GLORY- 2017-04-01
Vernon and District Performing Arts Center- A Midsummer Night's Dream- 2017-04-30
Vernon and District Performing Arts Centre- Canadiana Suite - 2017-05-06
Kelowna Community Theatre - Glenn Miller Orchestra- 2017-06-27

非常感谢任何提示。

我在没有日期的情况下尝试了它,只是按 event_title 和 place_name 进行分组,具有类似的奇怪输出,其中唯一的重复项是 field 名称(place_name)。

最佳答案

当您使用GROUP BY时,每个组只能获得一行。您选择的其他列将从该组中随机选择。

如果您想要查看重复组中的所有行,请将表与查找重复项的查询连接起来。

SELECT t1.*
FROM main AS t1
JOIN (SELECT place_name, start_date
FROM main
WHERE start_date >= '$todays_date'
GROUP BY start_date, place_name
HAVING count(*) > 1) AS t2
ON t1.place_name = t2.place_name AND t1.start_date = t2.start_date

关于mysql - 查找两列中重复的 mySQL 结果 - GROUP BY 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41754523/

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