我想通过说我尝试了几种不同的方法来使用JOIN,where子句和UNION,并且我无法弄清楚出了什么问题,因为这些查询需要很长时间才能执行。我有两个相对简单的查询,我想要组合成一个查询。
首先查询:
select count(distinct id) as shots, name, weapon
from attack
where category='Weapon'
group by name, weapon
order by name, weapon desc;
第二次查询:
firebase.auth().currentUser.getIdToken().then(function(idToken) {
// You can add the ID token to the header, or postBody, etc.
// For simplicity, let's append it to the URL.
window.location.assign('http://yourSignInSuccessUrl/?idToken=' + idToken);
}).catch(function(error) {
console.log(error);
});
两个查询都会生成一个包含几百行的结果表。我想要一个输出表,显示两者相交的镜头,命中,名称和武器。问题是,我一直在做的每一种方式都会使我的服务器的超时时间达到30秒,而单个查询只需要几分之一秒即可完成,所以我对我实际上做错了什么感到不知所措。
感谢您提供任何帮助 - 对我来说,mysql是一个爱好,所以我感谢任何专家花时间帮助我学习。
答案 0 :(得分:0)
在子查询中使用UNION
,然后在主查询中对它们进行分组。在联合中,您对来自另一个表的列使用虚拟值。
SELECT name, weapon, MAX(hits) AS hits, MAX(shots) AS shots
FROM (
select count(distinct id) as hits, 0 AS shots, name, weapon from damage group by name, weapon
UNION ALL
select 0 AS hits, count(distinct id) as shots, name, weapon from attack where category='Weapon' group by name, weapon
) AS x
GROUP BY name, weapon
ORDER BY name, weapon DESC