减少特定字段具有相似数据的记录集。 (MySQL)

发布于 2024-08-19 02:51:37 字数 585 浏览 8 评论 0原文

我有两个表,“讨论”和“讨论响应”。 “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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不服输 2024-08-26 02:51:37

This:

SELECT  *
FROM    discussion d
WHERE   discussion_date < colname 
        AND EXISTS
        (
        SELECT  NULL
        FROM    discussion_responses dr
        WHERE   dr.discussion_date > colname 
                AND dr.discussion_ID = d.discussion_ID
        )
ORDER BY
        d.discussion_title ASC

或 this:

SELECT  d.*
FROM    (
        SELECT  DISTINCT discussion_ID
        FROM    discussion_responses dr
        WHERE   dr.discussion_date > colname
        )
JOIN    discussion d
ON      d.discussion_ID = dr.discussion_ID
WHERE   d.discussion_date < colname 
ORDER BY
        d.discussion_title ASC

后一个查询可能会更快,因为第二个查询的前导表 (discussion_responses) 中使用的条件更具选择性。

discussion_responses(discussion_date,discussion_id)上创建索引。

This:

SELECT  *
FROM    discussion d
WHERE   discussion_date < colname 
        AND EXISTS
        (
        SELECT  NULL
        FROM    discussion_responses dr
        WHERE   dr.discussion_date > colname 
                AND dr.discussion_ID = d.discussion_ID
        )
ORDER BY
        d.discussion_title ASC

or this:

SELECT  d.*
FROM    (
        SELECT  DISTINCT discussion_ID
        FROM    discussion_responses dr
        WHERE   dr.discussion_date > colname
        )
JOIN    discussion d
ON      d.discussion_ID = dr.discussion_ID
WHERE   d.discussion_date < colname 
ORDER BY
        d.discussion_title ASC

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).

红颜悴 2024-08-26 02:51:37

如果您只对discussion_ID 感兴趣,则可以使用distinct 或group by。这是一个使用不同的示例:

SELECT      DISTINCT discussion_ID
FROM        discussion d
INNER JOIN  discussion_responses dr
ON          d.discussion_ID = dr.discussion_ID
WHERE       d.discussion_date < colname 
AND         dr.discussion_date > colname 

如果您还对其他列感兴趣,请在子查询中过滤不同的 id:

SELECT      *
FROM        discussion d
WHERE       d.discussion_ID IN (
    <query from above here>
)

If you're just interested in the discussion_ID, you could use distinct or group by. Here's an example with distinct:

SELECT      DISTINCT discussion_ID
FROM        discussion d
INNER JOIN  discussion_responses dr
ON          d.discussion_ID = dr.discussion_ID
WHERE       d.discussion_date < colname 
AND         dr.discussion_date > colname 

If you're also interested in other columns, filter on the distinct id's in a subquery:

SELECT      *
FROM        discussion d
WHERE       d.discussion_ID IN (
    <query from above here>
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文