mysql 重复选择并从重复项中减去1

发布于 2025-01-06 18:29:07 字数 1027 浏览 0 评论 0原文

我有一个有点奇怪的问题。 首先让我告诉你我无法改变数据库的制作方式。 在我之前的一些人确实做得很好,但也把一些事情搞砸了。

它是一个新闻网站,里面有编辑可以编辑其他人发布的新闻帖子。 编辑也可以自己发布新闻。

现在问题来了。

I have a table for news.

id   |  news_username    (who post news)


news_text Table

news_id  |  username  |  news_text   

(Here user: can be editor of the news or a person who  posted news)

User Table

username |  title 

该表中有一个新闻“id”和一个新闻“用户名”。 用户名是发布新闻的用户的用户名。 表内还分配了更多字段,但它们并不重要。

然后有一个表 news_text,其中放置了新闻文本。 该表有一个“news_id”和“username”字段。 这次的用户名是发布新闻的人的字段或编辑帖子的用户的用户名。

然后我有一个用户表,其中还有一个“用户名”字段和一个带有用户标题的“标题”字段。 在此

希望你仍然和我在一起。

简而言之,如果用户发布新闻文章,文本将位于“news_text”表内,当编辑编辑帖子时,更改后的文本将作为同一篇文章的新文本插入。 这样做是为了让原始发帖者看到他的帖子被修改了什么。

现在我的挑战来了。 我需要弄清楚如何获得编辑所做的编辑数量。 但是因为编辑本身可以发布新闻,这意味着我需要搜索用户名不等于原始发布者的所有新闻,以及在 news_text 表中查找重复项以查看编辑是否编辑了自己的帖子。

我真的希望人们能理解我需要做什么。 希望你能帮助我。


在评论中,马库斯·亚当斯向我指出了网站上最新编辑的使用方式。

这是通过日期/时间字段完成的。 进行编辑后,编辑内容将被插入 news_text 字段,并且新编辑内容将带有日期/时间戳。 这样,它将确定要抓取新闻项目的女巫文本。

希望这是清楚的

I have a bit of a strange question.
First let me tell you i cannot change the way the database is made.
Some people before me made a really good job of messing some things up pretty bad.

Its a news site and there are editors in that can edit the news posts that others post.
also editors can post news them self.

Now the question.

I have a table for news.

id   |  news_username    (who post news)


news_text Table

news_id  |  username  |  news_text   

(Here user: can be editor of the news or a person who  posted news)

User Table

username |  title 

In that table there is a news 'id' and a news 'username'.
The username being the username of the user posting the news.
there are also allot more fields inside the table but they are not important.

Then there is a table news_text and within it the text for the news is placed.
This table has a 'news_id' and 'username' field.
This time the username is the field of the person who posted the news OR the username of the user that edited the post.

Then i have a user table with also a 'username' field and a 'title' field with the title for the user.
In this

Hope your still with me.

In short if a user posts a news article the text will be inside the 'news_text' table and when an editor edits the post the altered text will be inserted as a new text for the same article.
This is done to let the original poster see what was altered to his post.

now comes my challenge.
I need to figure out how i can get the number of edits that an editor made.
BUT because the editor itself can post news that means that i need to search for all news where the username is not equal to the original poster and where it is look for duplicates in the news_text table to see of the editor has edited his own post.

i really hope people understand a little of what i need to do.
hope you can help me.


in the comment Marcus Adams pointed me at the how the latest edit was used on the website.

This is don through a date/time field.
When an edit is placed the edit will be inserted into the news_text field and there will be date/time stamp for the new edit.
And with that it will determine witch text to grab for the news item.

hope this is clear

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你没皮卡萌 2025-01-13 18:29:08

我建议最简单的方法是通过两个查询:

一个是查找用户所做的更改总数,

例如SELECT COUNT(*) FROM news_text WHERE username = {USERNAME} GROUP BY username

然后查找该用户创建的帖子总数,

例如SELECT COUNT(*) FROM news WHERE username = {USERNAME} GROUP BY username

并从另一个中减去一个。

或者对所有用户进行查询,然后取出您需要的用户。 (或者如果您确实想要的话,可以将它们合并到一个查询中)。

I would suggest that the easiest way to do this is in two queries:

One to find the total number of changes made by a user

e.g. SELECT COUNT(*) FROM news_text WHERE username = {USERNAME} GROUP BY username

And then one to find the total posts created by that user

e.g. SELECT COUNT(*) FROM news WHERE username = {USERNAME} GROUP BY username

And subtract one from the other.

Or do queries for all users and just take out the ones you need. (Or combine them into one query if you really want).

傲世九天 2025-01-13 18:29:08

据我了解,特定新闻项 (news_id) 有重复的 news_text 行。此外,news_text 表上有一个 edit_date 字段,并且您将获取具有最新 edit_date 的 news_text 以获取新闻项的最新版本。

我还了解您希望获得每个用户的编辑次数,不包括自我编辑。

这是一种解决方案。这是针对特定用户的:

SELECT COUNT(*) AS edits
FROM user u
JOIN news n
  ON n.username <> u.username
JOIN news_text nt1
  ON nt1.news_id = n.news_id
  AND nt1.username = u.username
JOIN news_text nt2
  ON nt2.news_id = n.news_id
  AND nt2.edit_date < nt1.edit_date
  AND nt2.username <> u.username
LEFT JOIN news_text nt3
  ON nt3.news_id = n.news_id
  AND nt3.edit_date > nt2.edit_date AND nt3.edit_date < nt1.edit_date
WHERE u.username = 'myuser'
  AND nt3.news_id IS NULL
  • 首先,它收集不是由该用户创作的所有新闻项目 (ON n.username <> u.username)
  • 然后收集所有 news_texts我们的用户编辑的每条新闻(ON nt1.news_id = n.news_id AND nt1.username = u.username
  • 然后它会收集上述内容的所有先前版本来自其他用户(ON nt2.news_id = n.news_id
    AND nt2.edit_date < nt1.edit_date AND nt2.用户名 <> u.username
  • 然后它会收集当前版本和前一个版本之间的所有版本,以便稍后排除(ON nt3.news_id = n.news_id
    AND nt3.edit_date > nt2.edit_date AND nt3.edit_date < nt1.edit_date
  • 然后它会过滤我们的用户(WHERE u.username = 'myuser'
  • 然后它会过滤掉当前和上一个之间的编辑,确保我们只拥有之前的版本 (nt3.news_id IS NULL)

As I understand it, you have duplicate news_text rows for a particular news item (news_id). Also, you have an edit_date field on news_text table, and you're getting the news_text with the latest edit_date to get the latest version of the news item.

I also understand that you want the to get the number of edits from each user, excluding self-edits.

Here's one solution. This is for a particular user:

SELECT COUNT(*) AS edits
FROM user u
JOIN news n
  ON n.username <> u.username
JOIN news_text nt1
  ON nt1.news_id = n.news_id
  AND nt1.username = u.username
JOIN news_text nt2
  ON nt2.news_id = n.news_id
  AND nt2.edit_date < nt1.edit_date
  AND nt2.username <> u.username
LEFT JOIN news_text nt3
  ON nt3.news_id = n.news_id
  AND nt3.edit_date > nt2.edit_date AND nt3.edit_date < nt1.edit_date
WHERE u.username = 'myuser'
  AND nt3.news_id IS NULL
  • First, it gathers all the news items that weren't authored by the user (ON n.username <> u.username)
  • Then it gathers all the news_texts for each of those news items that our user edited (ON nt1.news_id = n.news_id AND nt1.username = u.username)
  • Then it gathers all the previous versions for the above that are from other users (ON nt2.news_id = n.news_id
    AND nt2.edit_date < nt1.edit_date AND nt2.username <> u.username
    )
  • Then it gathers any versions between the current one and the previous one, for exclusion later (ON nt3.news_id = n.news_id
    AND nt3.edit_date > nt2.edit_date AND nt3.edit_date < nt1.edit_date
    )
  • Then it filters for our user (WHERE u.username = 'myuser')
  • Then it filters out edits that were in between the current and the previous, ensuring we only have the version just prior (nt3.news_id IS NULL)
眸中客 2025-01-13 18:29:07

如果我理解正确的话,这样的东西应该给出所有用户所做的编辑总数,但仅限于他们在不是自己的帖子上的位置

SELECT
    user.*,
    COUNT(*) AS edits

FROM user

// Join posts that aren't this users
INNER JOIN news
ON news.username != user.username

// Join edits for the above posts that are this users
INNER JOIN news_text
ON news_text.news_id = news.id
AND news_text.username != user.username

,并且如果您想选择特定的新闻文章

SELECT
    user.*,
    COUNT(*) AS edits

FROM user

// Join posts that aren't this users
INNER JOIN news
ON news.username != user.username
AND news.id = [[SPECIFIC ID]]

// Join edits for the above posts that are this users
INNER JOIN news_text
ON news_text.news_id = news.id
AND news_text.username != user.username

,或者如果您想的话,还可以使用 占位符查看特定用户对特定文章进行了多少次编辑

SELECT
    user.*,
    COUNT(*) AS edits

FROM user

// Join posts that aren't this users
INNER JOIN news
ON news.username != user.username
AND news.id = [[SPECIFIC ID]]

// Join edits for the above posts that are this users
INNER JOIN news_text
ON news_text.news_id = news.id
AND news_text.username != user.username

WHERE user.username = [[SPECIFIC USERNAME]]

编辑 另一种方法,如果您想统计某个用户发布的所有非原始帖子的帖子,即所有编辑,即使它们正在编辑他们自己的帖子

SELECT
    user.*,
    news.*,
    COUNT(*)-IF(news.username=user.username,1,0) AS edits
FROM user

// This join will give us all posts made by user
INNER JOIN news_text
ON news_text.username = user.username

// Also join the news id
INNER JOIN news
ON news_text.news_id = news.id

GROUP BY user.username, news.id

这将为每个用户返回 1 行根据 news.id 计算用户对其进行的编辑次数,因此要获取此值并返回总数,您可以这样做以按名称返回单个用户执行的编辑次数

SELECT
    username,
    sUM(edits)
FROM (
    SELECT
        news_text.username.username,
        COUNT(*)-IF(news.username=news_text.username,1,0) AS edits
    FROM news_text
    ON news_text.username = [[USER TO CHECK]]

    // Also join the news id
    INNER JOIN news
    ON news_text.news_id = news.id

    GROUP BY news.id
)

If I understand correctly, something like this should give the total number of edits all users have made but only where they were on posts that weren't themselves

SELECT
    user.*,
    COUNT(*) AS edits

FROM user

// Join posts that aren't this users
INNER JOIN news
ON news.username != user.username

// Join edits for the above posts that are this users
INNER JOIN news_text
ON news_text.news_id = news.id
AND news_text.username != user.username

And with a placeholder if you want to select a specific news article

SELECT
    user.*,
    COUNT(*) AS edits

FROM user

// Join posts that aren't this users
INNER JOIN news
ON news.username != user.username
AND news.id = [[SPECIFIC ID]]

// Join edits for the above posts that are this users
INNER JOIN news_text
ON news_text.news_id = news.id
AND news_text.username != user.username

Or maybe if you want to see how many edits a specific user has done on a specific article

SELECT
    user.*,
    COUNT(*) AS edits

FROM user

// Join posts that aren't this users
INNER JOIN news
ON news.username != user.username
AND news.id = [[SPECIFIC ID]]

// Join edits for the above posts that are this users
INNER JOIN news_text
ON news_text.news_id = news.id
AND news_text.username != user.username

WHERE user.username = [[SPECIFIC USERNAME]]

EDIT Alternative approach, if you want to count up all posts made by a user that ARE NOT original posts i.e. all edits, even if they are editing their own post

SELECT
    user.*,
    news.*,
    COUNT(*)-IF(news.username=user.username,1,0) AS edits
FROM user

// This join will give us all posts made by user
INNER JOIN news_text
ON news_text.username = user.username

// Also join the news id
INNER JOIN news
ON news_text.news_id = news.id

GROUP BY user.username, news.id

This will return 1 row per user per news.id counting the number of edits a user has made against it, so to take this and return totals you could instead do this to return the number of edits performed by a single user by name

SELECT
    username,
    sUM(edits)
FROM (
    SELECT
        news_text.username.username,
        COUNT(*)-IF(news.username=news_text.username,1,0) AS edits
    FROM news_text
    ON news_text.username = [[USER TO CHECK]]

    // Also join the news id
    INNER JOIN news
    ON news_text.news_id = news.id

    GROUP BY news.id
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文