简体   繁体   English

MYSQL具有不存在的条目

[英]MYSQL HAVING with nonexistent entries

i want to select a max of s2.maxcol or 0 if there are no entries. 我想选择最大s2.maxcol或0(如果没有条目)。 So far this works, but if there is no corresponding entry it is not returned: 到目前为止,这是可行的,但是如果没有相应的条目,则不会返回:

SELECT MAX( s2.maxcol) AS max_col, s1 . *
FROM table AS s1
LEFT JOIN table AS s2 ON s2.parent = s1.id
GROUP BY s1.id
HAVING max_col <100

But i also want to have the rows where the left join returns no corresponding entry (so max(s2.maxcol) should be 0. 但是我也想让左联接不返回任何对应条目的行(因此max(s2.maxcol)应该为0。

How can i solve that? 我该如何解决?

I just gave this a quick look and have to leave right now. 我只是快速浏览了一下,现在必须离开。 But maybe COALESCE might help. 但是也许COALESCE可能会有所帮助。 Here is the info 这是信息

Maybe something like this? 也许是这样的吗? (UNTESTED!) (未测试!)

SELECT COALESCE(MAX(s2.maxcol), 0) AS max_col, s1 . *
FROM table AS s1
LEFT JOIN table AS s2 ON s2.parent = s1.id
GROUP BY s1.id
HAVING max_col <100

Hope that helps. 希望能有所帮助。 Bye! 再见!

HAVING max_col <100更改为HAVING max_col is NULL or max_col <100 ,这可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM