SQL连接问题有两个表

时间:2016-04-19 01:21:52

标签: mysql sql

SELECT tablea.z, count(tablea.z), count(tableb.y)
FROM tablea
JOIN tableb
ON tablea.y = tableb.y
GROUP BY tablea.z;

我试图最终获得count(tableb.y)/count(tablea.z)

当我单独找到它们时,每个人都可以正常工作,但当我如上所示加入表格时,count(tablea.z)会变成count(tableb.y)

任何提示?

1 个答案:

答案 0 :(得分:0)

您需要能够彼此独立地评估2个表计数:

我可能有你想弄错的东西,但我希望这有助于你了解正在发生的事情

select 
   a.z, a.numrows / b.numrows as calc -- perhaps some error checking to avoid /0 error
from (select z, count(*) as numrows from tablea group by z) as a
    join 
 (select z, count(*) as numrows from tableb group by z) as b
     on a.z = b.z