谁能告诉我为什么这段代码:
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;