2014-01-10 192 views
0

我有這樣的查詢,如何將這兩個結果合併爲一個在mysql中?

SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE a.email_receiver_id = b.user_id 
AND a.email_sender_id= 20 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id 
UNION 
SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE a.email_sender_id = b.user_id 
AND a.email_receiver_id= 20 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id; 

它的輸出中是 -

user_id first_name last_name email_id     sharedFiles 
17  Mohan  Sharma  [email protected] 2    
17  Mohan  Sharma  [email protected] 1    

它顯示兩個不同的記錄。但我想這樣的輸出 -

user_id first_name last_name email_id     sharedFiles 
17  Mohan  Sharma  [email protected] 3 

我是新的mysql,所以請幫助我如何得到這個輸出。

謝謝。

回答

1
SELECT b.user_id,b.first_name, b.last_name, b.email_id, SUM(sharedFiles)as sharedFiles 
FROM(
SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE a.email_receiver_id = b.user_id 
AND a.email_sender_id= 20 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id 
UNION 
SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE a.email_sender_id = b.user_id 
AND a.email_receiver_id= 20 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id)x 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id; 
+0

做了一些編輯,它的工作,謝謝你 – Aamir

1

將您的查詢用作子查詢允許進行額外的分組和總結。請看下面的查詢:

SELECT (b.user_id,b.first_name, b.last_name, b.email_id, SUM(b.sharedFiles) 
FROM (
    SELECT b.user_id,b.first_name, b.last_name, b.email_id, COUNT(DISTINCT a.file_id) AS sharedFiles 
    FROM email_history a, users b 
    WHERE a.email_receiver_id = b.user_id 
    AND a.email_sender_id= 20 
    GROUP BY b.user_id,b.first_name, b.last_name, b.email_id UNION 
    UNION 
    SELECT b.user_id,b.first_name, b.last_name, b.email_id, COUNT(DISTINCT a.file_id) AS sharedFiles 
    FROM email_history a, users b 
    WHERE a.email_sender_id = b.user_id 
    AND a.email_receiver_id= 20 
    GROUP BY b.user_id,b.first_name, b.last_name, b.email_id; 
) AS b 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id 
+1

我已經在使用union了,請再檢查一次。 – Aamir

+0

我的不好,給我第二個修改我的答案 –

+0

@AamirSohail做這個工作嗎? –

1

用於管理UNION的結果是將它放在一個子查詢和一般的解決方法在外部函數做分組如下:

SELECT t.user_id, t.first_name, t.last_name, t.email_id, SUM(t.sharedFiles) 
FROM 
(SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE a.email_receiver_id = b.user_id 
AND a.email_sender_id= 20  
UNION 
SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE a.email_sender_id = b.user_id 
AND a.email_receiver_id=20) t 
GROUP BY t.user_id; 

然而,似乎您的確切請求可以放在帶有OR的單個查詢上。

SELECT b.user_id,b.first_name, b.last_name, b.email_id, 
COUNT(DISTINCT a.file_id) AS sharedFiles 
FROM email_history a, users b 
WHERE (a.email_receiver_id = b.user_id 
AND a.email_sender_id= 20) OR (a.email_sender_id = b.user_id 
AND a.email_receiver_id= 20) 
GROUP BY b.user_id,b.first_name, b.last_name, b.email_id 
相關問題