0

谁能告诉我为什么这段代码:

SELECT t.prob FROM 
(SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els) AS e2) AS t
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els2 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els2) AS e2)
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els3 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els3) AS e2);

产生与此相同的输出而不是“概率”的总和?

SELECT SUM(t.prob) FROM 
(SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els) AS e2) AS t
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els2 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els2) AS e2)
 UNION ALL (SELECT e1.evaled/IFNULL(NULLIF(e2.total,0),1) AS prob FROM 
    (SELECT COUNT(*) AS evaled FROM els3 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els3) AS e2);

(代码基本上为三个表 els、els2 和 els3 中的每一个创建一个仅包含一个值的列 prob,然后将它们中的三个组合成一个列,我希望从中得到它的 3 个元素的总和)

我想出了这个其他代码。它有效,而且是一个更清晰的陈述,所以没关系:

SELECT SUM(t.evaled/IFNULL(NULLIF(t.total,0),1)) as sumatory FROM
(SELECT evaled,total FROM
 (SELECT COUNT(*) AS evaled FROM els WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els) AS e2
UNION ALL SELECT * FROM
 (SELECT COUNT(*) AS evaled FROM els2 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els2) AS e2
UNION ALL SELECT * FROM
 (SELECT COUNT(*) AS evaled FROM els3 WHERE evals=0) AS e1 
     INNER JOIN (SELECT COUNT(*) AS total FROM els3) AS e2) as t;
4

1 回答 1

1

不知道你的解决方案出了什么问题,但我想我明白了你想要达到的目标。像这种方法怎么样:

SELECT 
 (SELECT COUNT(*)/(SELECT COUNT(*) FROM els) AS evaled FROM els WHERE evals=0)
 +
 (SELECT COUNT(*)/(SELECT COUNT(*) FROM els2) AS evaled FROM els2 WHERE evals=0) 
 +
 (SELECT COUNT(*)/(SELECT COUNT(*) FROM els3) AS evaled FROM els3 WHERE evals=0)
 /3

编辑:如果你想要平均,我猜你/3最后需要一个..

于 2012-04-15T13:10:07.713 回答