>最小示例
我有一个两列的table1
(id
,name
(
1 asdf
2 fdsa
3 qwerty
4 ytrewq
我还有一个三列table2
(id
、ref
、num
(
1 1 0.1
2 1 5.0
3 1 -3.9
4 2 2.4
5 2 -2.3
6 3 1.7
以下查询:
SELECT
table1.id,sum(num),
table1.name as anumber
FROM table1
LEFT JOIN table2 ON table1.id=table2.ref
GROUP BY table1.id;
给我一个结果,如下所示:
1 1.2 asdf
2 0.1 fdsa
3 1.7 qwerty
4 NULL ytrewq
我尝试的查询:
SELECT
table1.id,sum(num) as anumber,
table1.name
FROM table1
LEFT JOIN table2 ON table1.id = table2.ref
GROUP BY table1.id
HAVING anumber is null or anumber >= 1.0;
我的期望是我应该看到这些结果
1 1.2 asdf
3 1.7 qwerty
4 NULL ytrewq
但实际上我根本没有得到任何结果。我如何在此处实际格式化我的查询?
好的,所以查询实际上按预期工作。我从实际设置中缺少一些使其无法正常工作的东西。所需的结果是查找在另一个表指定的属性上具有最小值的项,或者该属性完全未知的项。
它具有您想要的确切输出:
SELECT
table1.id,sum(table2.num) as t2,
table1.name as anumber
FROM table1
LEFT JOIN table2 ON table1.id=table2.ref
GROUP BY table1.id
having t2 is null or t2 >= 1.0;
输出
id | t2 | anumber
1 1.2 asdf
3 1.7 qwerty
4 NULL ytrewq