减少特定字段具有相似数据的记录集。 (MySQL)
我有两个表,“讨论”和“讨论响应”。 “discussion_responses”中使用“discussion”中的唯一 ID 来标识对论坛中原始帖子的响应。我创建了以下查询来提取具有新回复但未链接到新主题的帖子。两个表都有一个用于添加日期的字段,“colname”是 Dreamweaver 的变量名称,表示从另一个表检索到的访问者上次登录日期:
SELECT *
FROM discussion, discussion_responses
WHERE discussion.discussion_date < colname
AND discussion_responses.discussion_date > colname
AND discussion.discussion_ID = discussion_responses.discussion_ID
ORDER BY discussion.discussion_title ASC
我的问题是,如果对原始帖子做出了多个回复,我自然会得到该讨论 ID 有多个结果。谁能告诉我如何消除包含相同discussion_ID 的后续行?选择哪条记录并不重要,重要的是我可以获得与其关联的新回复的任何主题的 ID。谢谢。
I have two tables, 'discussion' and 'discussion_responses'. A unique ID from 'discussion' is used in 'discussion_responses' to identify the response to the original post in a forum. I created the following query to extract the posts that have NEW replies, but are NOT linked to new topics. Both tables have a field for the date added and 'colname' is Dreamweaver's variable name for the visitor's last log-in date retrieved from another table:
SELECT *
FROM discussion, discussion_responses
WHERE discussion.discussion_date < colname
AND discussion_responses.discussion_date > colname
AND discussion.discussion_ID = discussion_responses.discussion_ID
ORDER BY discussion.discussion_title ASC
My problem is that where more than one reply has been made to the original post, I naturally get more than one result for that discussion ID. Can anyone please advise me as to how I can eliminate subsequent rows containing the same discussion_ID? It doesn't matter which record is chosen, only that I get the ID of any topic that has had a new reply associated with it. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
This:
或 this:
后一个查询可能会更快,因为第二个查询的前导表
(discussion_responses)
中使用的条件更具选择性。在
discussion_responses(discussion_date,discussion_id)
上创建索引。This:
or this:
The latter query will probably be faster, since the condition used in the second query's leading table
(discussion_responses)
is more selective.Create an index on
discussion_responses (discussion_date, discussion_id)
.如果您只对discussion_ID 感兴趣,则可以使用distinct 或group by。这是一个使用不同的示例:
如果您还对其他列感兴趣,请在子查询中过滤不同的 id:
If you're just interested in the discussion_ID, you could use distinct or group by. Here's an example with distinct:
If you're also interested in other columns, filter on the distinct id's in a subquery: