以下两个查询完全相同,除了国家名称。在查询 1 中是“马来西亚”,在查询 2 中是“印度”。
查询本身是错误的;由于我ac.country
在第一个子查询中使用“”,而我实际上需要使用“ ass.country
”,但我仍然在查询 1 中得到结果,而在查询 2 中得到 NULL。
有人可以解释为什么会这样。
查询一:
SELECT DATE(DAY) AS daycust,
(SELECT SUM(Quantity) AS webss
FROM ACQ AS ass
WHERE ass.day = ac.day
AND ac.country IN (SELECT DISTINCT aam.country_name FROM accounts AS aam)
AND country = 'Malaysia') AS webss
FROM analytics_country AS ac
WHERE DAY BETWEEN '2016-07-01' AND '2016-08-02'
AND ac.country IN (SELECT DISTINCT aam.country_name FROM accounts AS aam)
AND country = 'Malaysia'
GROUP BY daycust
ORDER BY daycust
查询 2:
SELECT DATE(DAY) AS daycust,
(SELECT SUM(Quantity) AS webss
FROM ACQ AS ass
WHERE ass.day = ac.day
AND ac.country IN (SELECT DISTINCT aam.country_name FROM accounts AS aam)
AND country = 'India') AS webss
FROM analytics_country AS ac
WHERE DAY BETWEEN '2016-07-01' AND '2016-08-02'
AND ac.country IN (SELECT DISTINCT aam.country_name FROM accounts AS aam)
AND country = 'India'
GROUP BY daycust
ORDER BY daycust
注意:我还想补充一点,不同版本的 MySql 的输出会有所不同。
In Server version: "5.5.30-tokudb-7.0.1-MariaDB-log" -> Both the queries work fine.
In Server version: "5.5.41-tokudb-7.5.5-MariaDB - MariaDB Server" -> Only Query 1 works fine.