このコードの理由を教えてください:
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);
(コードは基本的に、3 つのテーブル els、els2、els3 のそれぞれに値を 1 つだけ含む列 prob を作成し、それらの 3 つを 1 つの列に結合して、そこから 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;