任何人都可以告诉我为什么这段代码:
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);
产生与此相同的输出而不是'prob'的总和?
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);
(代码基本上创建了一个列prob,它只包含三个表els,els2和els3中的每一个的一个值,然后将它们中的三个组合成一个列,我想要它的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;
答案 0 :(得分: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
..