Skip to content
Advertisement

SQL Joins not working correctly when no data present

I’ve got the following code, where I build a tag from an entity

Each tag has an id, name, description and how many questions it has, how many it has in a day, and how many it has in a week.

The problem is, I can’t seem to display tags that have no questions tied to them, and also if a tag doesn’t have any daily or weekly question, the tag itself is also not displayed. I tried writing a coalesce() in the constructor itself, with the arguments of count and then 0, but it does not seem to work, and coalesce returns an int instead of a long.

How would I make tags with no questions or with no recent(in a day’s or week’s time) questions still get displayed? Preferably with these null values being replaced with a 0

EDIT: Here are some example datasets

and the expected tag json:

Advertisement

Answer

For anyone who stumbled on this question and is looking for an answer, you should use ON clause instead of WHERE, and also the ON clauses should be right after the LEFT JOINS:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement