mysql 重复选择并从重复项中减去1
我有一个有点奇怪的问题。 首先让我告诉你我无法改变数据库的制作方式。 在我之前的一些人确实做得很好,但也把一些事情搞砸了。
它是一个新闻网站,里面有编辑可以编辑其他人发布的新闻帖子。 编辑也可以自己发布新闻。
现在问题来了。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议最简单的方法是通过两个查询:
一个是查找用户所做的更改总数,
例如
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).
据我了解,特定新闻项 (news_id) 有重复的 news_text 行。此外,news_text 表上有一个 edit_date 字段,并且您将获取具有最新 edit_date 的 news_text 以获取新闻项的最新版本。
我还了解您希望获得每个用户的编辑次数,不包括自我编辑。
这是一种解决方案。这是针对特定用户的:
ON n.username <> u.username
)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:
ON n.username <> u.username
)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.username <> 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
)如果我理解正确的话,这样的东西应该给出所有用户所做的编辑总数,但仅限于他们在不是自己的帖子上的位置
,并且如果您想选择特定的新闻文章
,或者如果您想的话,还可以使用 占位符查看特定用户对特定文章进行了多少次编辑
编辑 另一种方法,如果您想统计某个用户发布的所有非原始帖子的帖子,即所有编辑,即使它们正在编辑他们自己的帖子
这将为每个用户返回 1 行根据 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
And with a placeholder if you want to select a specific news article
Or maybe if you want to see how many edits a specific user has done on a specific article
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
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