比较两个MySQL表中的值

soat7uwm  于 2022-12-03  发布在  Mysql
关注(0)|答案(1)|浏览(91)

当前方法:

当前查询1

SELECT
a.user,ui.contact_fname, ui.contact_lname,a.uid, COALESCE(pu.name,'N/A') AS UserPolicy,COALESCE(count(ea.user_ack),0) AS ack_count
FROM master_access.accounts a
RIGHT JOIN master_events.events_cleared ea ON a.uid=ea.user_ack
INNER JOIN master_biz.user_information as ui on ui.uid=a.uid
LEFT JOIN `master`.policies_users pu ON (a.template_id=pu.policy_id AND a.template_id!=0)
WHERE YEAR(ea.date_ack)=YEAR(NOW()) and MONTH(ea.date_ack)=10 and a.uid in (120,119,125,128,123,117,122,118,121,127)
GROUP BY a.user
ORDER BY COUNT(ea.user_ack) DESC

当前查询1 -输出

enter image description here

当前查询2

SELECT
a.user,ui.contact_fname, ui.contact_lname,a.uid, COALESCE(pu.name,'N/A') AS UserPolicy,COALESCE(count(ea.user_ack),0) AS ack_count
FROM master_access.accounts a
RIGHT JOIN master_events.events_active ea ON a.uid=ea.user_ack
INNER JOIN master_biz.user_information as ui on ui.uid=a.uid
LEFT JOIN `master`.policies_users pu ON (a.template_id=pu.policy_id AND a.template_id!=0)
WHERE YEAR(ea.date_ack)=YEAR(NOW()) and MONTH(ea.date_ack)=10 and a.uid in (120,119,125,128,123,117,122,118,121,127)
GROUP BY a.user
ORDER BY COUNT(ea.user_ack) DESC

当前查询2 -输出

enter image description here

所需输出

基本上是将两个输出添加到一个视图中,如果“用户ID”匹配,则添加值。有人可以帮助完成此操作吗?

bkhjykvo

bkhjykvo1#

master_access.accounts表为基表。将两个查询都左联接到该表。筛选至少有一个子查询行匹配的行。生成输出列列表。

SELECT *        -- build needed columns list
FROM master_access.accounts
LEFT JOIN ( {query 1 text} ) AS subquery1 USING (uid)
LEFT JOIN ( {query 2 text} ) AS subquery2 USING (uid)
WHERE COALESCE (subquery1.uid, subquery2.uid) IS NOT NULL

在输出列表中-取不带表别名的uid列,而不是所有其他列。

相关问题