2012-02-17 75 views
1

我有點奇怪的問題。首先讓我告訴你,我不能改變數據庫的製作方式。 在我之前的一些人做了一些非常糟糕的工作。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'和'用戶名'字段。 這次用戶名是發佈新聞的人的字段或編輯帖子的用戶的用戶名。

然後,我有一個用戶表,還有一個'用戶名'字段和'標題'字段與用戶的標題。 在此

希望你仍然和我在一起。

簡而言之,如果用戶發佈新聞文章,文本將位於'news_text'表格內,並且當編輯者編輯帖子時,被更改的文本將作爲同一文章的新文本插入。 這樣做是爲了讓原始海報能夠看到他的帖子被修改了什麼。

現在來我的挑戰。 我需要弄清楚如何獲得編輯器編輯的編輯數量。 但是,因爲編輯器本身可以發佈新聞,這意味着我需要搜索所有消息,其中用戶名不等於原始海報,並且它在news_text表中查找重複項以查看編輯者編輯他自己的帖子。

我真的希望人們瞭解我需要做的一些事情。 希望你能幫助我。


在評論中馬庫斯亞當斯指出我如何在網站上使用最新的編輯。

這是通過日期/時間字段。 放置編輯時,編輯將被插入到news_text字段中,並且新編輯將會有日期/時間戳記。 然後,它將確定女巫的文字來抓取新聞。

希望這是明確的

+0

怎麼樣你決定爲特定的news_id顯示哪個news_text行?最高ID?最近的日期? – 2012-02-17 13:45:32

+0

請務必查看我的答案,其中包含自我編輯。 – 2012-02-17 14:54:09

回答

1

如果我理解正確的話,這樣的事情應該給編輯的所有用戶都發,但只有在他們對本身不是

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 

或者,如果你想看看有多少編輯一個特定的用戶已經在specifi做也許ç文章

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 
) 
+0

不確定他是否希望所有編輯都包含在內或不包含tbh,但很容易完成 – 2012-02-17 13:54:48

+0

我認爲只需稍微調整一下這些代碼就可以爲我工作:)幫助。當我有它的工作,我會讓你知道。再次thks。並編輯自己的新聞項目。我認爲代碼將工作一點點調整;) – Controvi 2012-02-17 13:56:37

+0

在那裏我已經添加了幾個建議,也許能夠具體做你的後面:) – 2012-02-17 14:03:13

0

我建議,最簡單的方法做,這是在兩個查詢:

一個找到用戶

例如所做的更改總數SELECT COUNT(*) FROM news_text WHERE username = {USERNAME} GROUP BY username

然後一個找到該用戶

例如產生的總帖SELECT COUNT(*) FROM news WHERE username = {USERNAME} GROUP BY username

然後從另一箇中減去一個。

或者爲所有用戶查詢,只是拿出你需要的。 (或者如果你真的想把它們合併成一個查詢)。

0

據我所知,你有一個特定的新聞項目(news_id)重複的news_text行。另外,你在news_text表上有一個edit_date字段,並且你得到news_text和最新的edit_date來獲取最新版本的新聞項目。

我也明白,您希望獲得來自每個用戶的編輯數量,不包括自編輯。

下面是一個解決方案。這是一個特定的用戶:

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.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
+0

編輯有關edit_date字段的答案,用於確定最新版本而不是auto_increment。 – 2012-02-17 14:35:51

+0

將其中一個'WHERE'子句移入它應該出現的'ON'子句中。 – 2012-02-17 14:47:18

+0

我已經優化了一些,假設第一個news_text始終來自原作者。 – 2012-02-17 14:51:58