我有这样的查询,
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 mohan.guptademo@gmail.com 2
17 Mohan Sharma mohan.guptademo@gmail.com 1
它显示两个不同的记录。但我想要这样的输出 -
user_id first_name last_name email_id sharedFiles
17 Mohan Sharma mohan.guptademo@gmail.com 3
我是mysql的新手,所以请帮助我如何获得此输出。
谢谢。
答案 0 :(得分: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 :(得分: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;
答案 2 :(得分: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